[Tips] Entity Framework entity-type include

Source: Internet
Author: User

If lazy loading enable = true is not set in the Entity Framework, the handler must handle the loading of the metadata, for example, using load or include, however, because the include parameter number is string, it is very difficult for a person to find the parameter easily after tablename is changed with string, so I want to introduce an extension method.

// Adventureworkslt2008r2entities context = new adventureworkslt2008r2entities (); var customer = context. customer. include ("salesorderheader "). tolist (); // The portable mode adventureworkslt2008r2entities context = new adventureworkslt2008r2entities (); var customer = context. customer. include (x => X. salesorderheader ). tolist ();

Note:

This method is disabled after collection is used. For example, in the previous example, the salesorderheader is a collection and cannot be used. salesorderheader. salesorderdetail: Why is it the hour? Because the younger brother hasn't come up with a good idea, use X. salesorderheader [0]. salesorderdetail or X. salesorderheader. first (). salesorderdetail.

Source code
Public static iqueryable <t> include <t> (this objectset <t> source, expression <func <t, Object> path) where T: class {// steal steps, for example, expression is X => X. customer. customeraddress, directly following tostring from the first. split, take the following customer. customeraddress string Spath = path. body. tostring (); Spath = Spath. substring (Spath. indexof ('. ') + 1); Return source. include (Spath );}

The code above is more efficient than the hacker, so we should analyze the expression than the regular expression.

Public static iqueryable <t> include <t> (this objectset <t> source, expression <func <t, Object> path) where T: class {stringbuilder pathbuilder = new stringbuilder (); memberexpression pro = path. body as memberexpression; while (pro! = NULL) {// exprssion is a string column that is similar to the result column. It is written forward from the end of statemant, for example, x => X. customer. customeraddress // path. the body is customeraddress // The expression of customeraddress is customer // The expression of customer is X pathbuilder. insert (0 ,". "+ pro. member. name); Pro = pro. expression as memberexpression;} return source. include (pathbuilder. tostring (1, pathbuilder. length-1 ));}

Note:

I did not perform the checking operation check. I want to say that the Entity Framework will also cause an exception, so there will be no more issues on my own.

When 1 Model

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.