Dynamic Linq - Expression - Contains

CSharp · Console

/Program.cs

// Dynamic LINQ
// Doc: https://linq-dynamic.net/dynamic-extensions-methods

// @nuget: System.Linq.Dynamic.Core

using System;
using System.Linq;
using System.Collections.Generic;
using System.Linq.Dynamic.Core;
					
public class Program
{
	public static void Main()
	{
		  Dictionary<string, object> Properties =new Dictionary<string, object>();
		Properties.Add("p10",2);
		Properties.Add("p11",4);
		Properties.Add("p135","Test1");
		Properties.Add("p17","2021-10-10T09:48:27.9708889Z");
		
		 Dictionary<string, object> Properties1 =new Dictionary<string, object>();
		Properties1.Add("p10",20);
		Properties1.Add("p11",40);
		Properties1.Add("p135","non");
		Properties1.Add("p17","2021-12-10T09:48:27.9708889Z");
		
		 List<Customer> customers = new List<Customer>()
		 {
			 new Customer() { CustomerID = 1, Name = "xyz", Properties=Properties},
			 new Customer() { CustomerID = 2, Name = "Gail",Properties=Properties1},
			 new Customer() { CustomerID = 3, Name = "Gigi N Matthew",Properties=Properties},
			 new Customer() { CustomerID = 4, Name = "Michael Raheem",Properties=Properties},
			 new Customer() { CustomerID = 5, Name = "Janice Galvin",Properties=Properties},
			 new Customer() { CustomerID = 6, Name = "Michael Sullivan",Properties=Properties1},
			 new Customer() { CustomerID = 7, Name = "David Bradley",Properties=Properties1},
			 new Customer() { CustomerID = 8, Name = "David	Benshoof",Properties=Properties1},
			 new Customer() { CustomerID = 9, Name = "Peter	Gilbert",Properties=Properties1},
			 new Customer() { CustomerID = 10, Name = "Rebecca Bradley",Properties=Properties1},
		 };
		
		//var list = customers.AsQueryable()
		//	.Where("np(Name)==\"xyz\" or np(Name)==\"Gail\"")
			//.Where("Int32(Properties[\"p10\"])>2 and (Properties.ContainsKey(\"p11\") and Int32(Properties[\"p11\"])>5) and Convert.ToString(Properties[\"p135\"]).Contains(\"non\")")
			//.Where("Convert.ToDateTime(Properties[\"p17\"])>Convert.ToDateTime(\"2021-05-10T09:48:27.9708889Z\")")

		//	.ToList();
		
			
		var tttt= customers.AsQueryable()			
			//.Where("@0.Contains(it.CustomerID)", new List<int>() { 1, 2,9 })
			//.Where("CustomerID==1 or CustomerID==2")
			//.Where("CustomerID In (11) ")
			.Where("Name In (\"Rebecca Bradley\") ")
			.ToList();
		
		foreach (var customer in tttt)
        {
			Console.WriteLine(customer.Name+ " "+ customer.Properties["p10"]);
		}
	}
	
	public class Customer
	{
		public int CustomerID { get; set; }
		public string Name { get; set; }
		public string Description { get; set; }
		public DateTime LastLogin { get; set; }
		public Boolean IsActive { get; set; }
		public Dictionary<string, object> Properties { get; set; }	
	}
	
}
;