CSharp · Console
// C# Eval Expression
// Doc: https://eval-expression.net/linq-dynamic
// @nuget: Z.Expressions.Eval
/*
## LINQ ToArrayDynamic Method
The LINQ ToArrayDynamic creates an Array from an IEnumerable.
In this example, we will create a list of product, filter them dynamically by their type, and return the result as an array.
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 product4 = new Product() { Name = ".NET Fiddle", Url = "https://dotnetfiddle.net/", Type = "Free" };
var product5 = new Product() { Name = "SQL Fiddle", Url = "https://sqlfiddle.com/", Type = "Free" };
var product6 = new Product() { Name = "ZZZ Code AI", Url = "https://zzzcode.ai/", Type = "Free" };
var list = new List<Product>() { product1, product2, product3, product4, product5, product6 };
var toArray = list.WhereDynamic("x => x.Type == 'Paid'").ToArrayDynamic();
FiddleHelper.WriteTable("1 - toArray:", toArray);
}
public class Product
{
public string Name { get; set; }
public string Url { get; set; }
public string Type { get; set; }
}
}