Application background: Take the goods as an example, in the basic data to configure the judgment rules of the goods, according to this rule to filter out the eligible set.
Create Goods class
public class Product {public string Name {get; set;} public string Code {get; set;} public string Unit {get; set;} }
Main implementation Methods
public void GetProducts () {#region Create list actual app to take value from database var products = new[] { New {name = "Item 1", Code = "001", unit = "}, new {name =" Item 2 ", Code =" 002 ", Unit =" Piece "}, New {name = "Item 3", Code = "003", unit = "Bottle"}, new {name = "Item 4", Code = "004", Unit = "One"}, New {Name = "Item 1", Code = "005", Unit = "Taiwan"},}; list<product> lsproducts = new list<product> (); foreach (var q in products) {Product Product = new Product (); Product. Name = Q.name; Product. Code = Q.code; Product. Unit = Q.unit; Lsproducts.add (product); } #endregion//Gets to the rule of judgment rule for Code and Unit where name is the fixed condition string condition = "Code,unit"; string[] Arraycondition = condition. Split (', '); Query Results list<product> Result = Lsproducts.where (A + = Filter (A, Arraycondition, "Item 1", "005", "")). ToList (); 0//Simply judge code where name is a fixed condition string condition2 = "code"; Arraycondition = Condition2. Split (', '); result = Lsproducts.where (A = = Filter (A, arraycondition, "Goods 1", "005", "")). ToList ();//1//No judgment rule where name is a fixed condition string condition3 = ""; Arraycondition = Condition3. Split (', '); result = Lsproducts.where (A = = Filter (A, arraycondition, "Goods 1", "005", "")). ToList ();//2} public bool Filter (product product, string[] arraycondition, string name, string code, Strin G unti) {bool result = false; result = (product. name = = name) && (!arraycondition.contains ("Code") true:product. Code = = code) && (!arraycondition.contains ("Unit") true:product. Unit = = unti); return result; }
[C #] LINQ Dynamic Condition Query