C# Eval - LINQ Dynamic - LINQ SelectDynamic Method

CSharp · Console

/Program.cs

// C# Eval Expression
// Doc: https://eval-expression.net/linq-dynamic

// @nuget: Z.Expressions.Eval

/*
## LINQ SelectDynamic Method

The LINQ SelectDynamic projects each element of a sequence into a new form. You can use a dynamic string expression to select a specific column.

In this example, we will dynamically select some product properties, and returns an anonymous list.

See more: https://eval-expression.net/linq-dynamic
*/

using System;
using System.Collections.Generic;
using System.Linq;
using Z.Expressions;

public class Program
{
	public static void Main()
	{
		var product1 = new Product() { Name = "C# Eval Expression", Url = "https://eval-expression.net/", Type = "Paid" };
		var product2 = new Product() { Name = "Dapper Plus", Url = "https://dapper-plus.net/", Type = "Paid" };
		var product3 = new Product() { Name = "Entity Framework Extensions", Url = "https://entityframework-extensions.net/", Type = "Paid" };
		
		var list = new List<Product>() { product1, product2, product3 };
		var anonymousList = list.SelectDynamic("x => new { x.Name, x.Url }").ToList();
		
		FiddleHelper.WriteTable("1 - anonymousList:", anonymousList);
	}
	
	public class Product
	{
		public string Name { get; set; }
		public string Url { get; set; }
		public string Type { get; set; }
	}
}

;