Expression tree Use (iv)

Source: Internet
Author: User
Tags instance method
If you do not really put down the past, there will be no tomorrow. Program.cs
 Class Program {static void Main (string[] args) {string[] companies = {"Integrated Instant messenger", "Alpine ski Home", "Shaoxing Srich", "City Power and Lighting", "Coho Winery", "intruders from all over the world", "Graphic Design Institute", "Adventure Project", "Humongous Insurance", "Woodgrove Bank", "Margie Travel", "

            Northwind Traders, "Blue Yonder Airlines", "Trey Research", "Telephone Company", "Wing-tip Toys", "Lucerne published", "fourth session Coffee"}; The IQueryable data to query iqueryable<string> Queryabledata = companies.

            Asqueryable<string> ();

            Expression tree parameter ParameterExpression PE = Expression.parameter (typeof (String), "Company"); -------------------------------------------------------------------------------------------------------//WH Ere (company =>. ToLower () = = "Coho Winery" | | Company.
            Length > 8)//Create a methodcallexpression that represents a call to an instance method without parameters. Create a presentation expression "company."
            ToLower () = = "Coho Winery" expression tree. Expression left = Expression.call (PE, typeof (String). GetMethod ("ToLower", System.Type.EmptyTypes));
            Expression right = Expression.constant ("Coho Winery");

            Expression e1 = Expression.equal (left, right); Create a presentation expression "company." length> 8 expression Tree left = Expression.property (PE, typeof (String).
            GetProperty ("Length"));
            right = Expression.constant (8, typeof (int));

            Expression e2 = Expression.greaterthan (left, right); Creates a binaryexpression that evaluates the condition//combination expression tree of the second operand to create a presentation expression, only if the first operand evaluates to False (company. ToLower () = = "Coho Winery" | | Company.
            Length> 8) "is an expression tree.

            Expression predicatebody = Expression.orelse (e1, E2); Create a representation expression Queryabledata.where company =>. ToLower () = = "Coho Winery" | | Company.
            Length> 8) "is an expression tree.
                Methodcallexpression wherecallexpression = Expression.call (typeof (Queryable), "Where", New type[] {queryabledata.elementtype}, queryabledata.expression, Expressi On. Lambda<func< string, bool>> (Predicatebody, new parameterexpression[] {PE}); -------------------------------------------------------------------------------------------------------//Create An expression tree ' wherecallexpression that represents an expression.
                By (company => Company) ' Methodcallexpression orderbycallexpression = Expression.call ( typeof (Queryable), "by", new type[] {queryabledata.elementtype, queryabledata.elemen Ttype}, Wherecallexpression, Expression.lambda<func<string, string>> (PE, new



            parameterexpression[] {PE})); //-------------------------------------------------------------------------------------------------------


            //
            Creates an executable query from the expression tree.

            iqueryable<string> results = queryabledata.provider.createquery<string> (orderbycallexpression);
         Enum result foreach (string company in results) {       Console.WriteLine (company);

        } console.readline (); }
    }
run the results as shown in figure:

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.