Change uncertainty to confirmation ~ There is no impact on the performance of the two writing methods of LINQ query. The optimized query should be "On-Demand query"

Source: Internet
Author: User

Back to directory

If you want to perform a one-to-multiple composite query in LINQ, use join into directly in the query or use the let keyword, of course, the dynamic value assignment when an object is created does not affect the query performance. The results of the two queries are the same.

I. assign values to objects

 VaR LINQ = From Data In this . GetModel () Join Data2 In Iwebdepartmentsrepository. GetModel () On Data. initialize mentid Equals Data2.departmentid Select New  Webmanageusers_ext {Manageuserid = data. manageuserid, loginname = data. loginname, webdepartments_extend = data2,   Webmanageuser_webmanageroles_extend =NewWebmanageuser_webmanagerolesrepository(). GetModel (). Where (I => I. manageuserid = data. manageuserid), Avatarurl = data. avatarurl ,};

Use the let keyword in the statement

 VaR LINQ = From Data In this . GetModel ()Join Data2 In Iwebdepartmentsrepository. GetModel () On Data. initialize mentid Equals Data2.departmentid LetList =NewWebmanageuser_webmanagerolesrepository(). GetModel (). Where (I => I. manageuserid = data. manageuserid)                         Select New  Webmanageusers_ext {Manageuserid = data. manageuserid, loginname = data. loginname, webdepartments_extend = data2,   Webmanageuser_webmanageroles_extend = List, Avatarurl = data. avatarurl ,};
 
The results of the two SQL analyzers are the same.
 
Of course, the best way to query the LINQ is to "query on demand". That is to say, select and select fields for the fields used,CodeIt may be like this:
 VaR LINQ = From Data In DB. webmanageusers Join Data2 In DB. webscripts On Data. initialize mentid Equals Data2.departmentid Let List1 = dB. webmanageuser_webmanageroles.where (I => I. manageuserid = data. manageuserid). Select (j => New  Webmanageuser_webmanageroles_ext {Manageroleid = J. manageroleid, manageuserid = J. manageuserid }) Select New  Webmanageusers_ext {Manageuserid = data. manageuserid, loginname = data. loginname, realname = data. realname, webdepartments_extend = New  Webdepartments_ext {Departmentname = data2.departmentname}, webmanageuser_webmanageroles_extend = list1 ,};
Back to directory

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.