CSharp · Console
// C# Eval Expression
// Doc: https://eval-expression.net/linq-dynamic
// @nuget: Z.Expressions.Eval
/*
## LINQ SelectToListDynamic Method
The LINQ SelectToListDynamic projects each element of a sequence into a new form and create a List from it. You can use a dynamic string expression to select a specific column.
In this example, we will dynamically select some product properties, cast our list as queryable (a queryable is required for this method), 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.AsQueryable().SelectToListDynamic("x => new { x.Name, x.Url }");
FiddleHelper.WriteTable("1 - anonymousList:", anonymousList);
}
public class Product
{
public string Name { get; set; }
public string Url { get; set; }
public string Type { get; set; }
}
}