[C #] LINQ Dynamic Condition Query

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.