Dynamic stitching liinq using expression to construct dynamic LINQ statements

Source: Internet
Author: User

Recently in the dynamic Construction of LINQ statements, from the Internet to find a lot, most of them are based on a table of one of the fields, so that the results from the internet can be searched a lot. But if there is a foreign key table, you need to dynamically construct the field in the Foreign key table, then the problem comes, learn excavator which strong? Oh, no, what should I do?

On the dynamic Construction of LINQ, http://www.cnblogs.com/blusehuang/archive/2007/07/13/816970.html the article has been described very clearly. I don't have to say much.

Among them, the key code is:

Expression con = expression.call (     typeof(T). GetProperty (key)),     typeof(string). GetMethod ("Contains"newtypeof(string)}),     expression.constant (Parameters[key]));

When we have a table, as follows:

 Public class test{    publicint a{get;  Set;}      Public string b{get;  Set;}      Public Test2 T2 {get;  Set;}}

There are also Test2 tables:

 Public class test2{    publicint id{get;  Set;}      Public string name{get;  Set;}}

To query from the test table to a LINQ statement in the Test2 table, this is the case.

DBContext.Entities.Where (x=>x.t2. Name.contains ("a"));

So, the question is, which is the best place to learn the excavator? Well, no, how do you turn into a dynamic???

A single dynamic construct is still clear and understandable,

Expression.property (c, typeof(T). GetProperty (key))

This code means reading a field under the test type, which should be Test2, and we'll change it to:

Expression.property (c, typeof (Test1). GetProperty (TEST2))

Well, right, the idea is correct, the next is more critical, I want to continue to find T2 under the Name property how to do it?? After I had debugged it, I found a secret.

The final code:

varmember = Expression.property (c,typeof(Test). GetProperty ("T2"));varMember2 = Expression.property (Member,typeof(TEST2). GetProperty ("Name")); Expression Con=Expression.call (Member2,typeof(string). GetMethod ("Contains",NewType[] {typeof(string)}), Expression.constant ("a") );

In this way, the code generated by its LINQ expression is c=>c.t2. Name.contains ("a"). is not so easy, solve the relationship between the two tables, the other problem is to say, if you do not know, please knock yourself an example, F5 debugging. Not successful, all over the adult.

Don't thank me, my name is red scarf.

Dynamic stitching liinq using expression to construct dynamic LINQ statements

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.