Self-writing SQL script query results in EF (for complex SQL logic to improve query efficiency)

Source: Internet
Author: User

Shortly before the project was developed, the Entity Framework was used to process data. Because I am not too much research on EF, but will use only, but in one need to query the results need to correlate 3, 4 tables to query the results, and query efficiency is also required. But personally, there is a situation in which the original SQL statement is used to query more controllable (perhaps there is a better way for EF to solve this kind of problem, but I only think of it as a stupid method). It also expands a method to specifically query custom-written SQL statements. The code is as follows:

 Public List<t> executestorequery (stringparamsobject[] parameters)        {             using (var db = getentity ())            {                var objectList = db. Executestorequery<t>(CommandText, parameters). ToList ();                 return objectList;            }        }

The previous segment of the code in DB. The Executestorequery<t> () method is a method that is already defined in EF and is visible for this condition.

The specific invocation method is as follows:

   Public List<classinfo> getclassinfolist ()        {            conststring@ " your SQL ";             // this way you can determine whether or not to make a parameterized query based on your SQL needs            . return  This . Executestorequery (SQL);        }    

Using this method, you can customize the model without having to use the EF template to automatically help you build the model, but the fields in the SQL results of the query must exist in the given type.

If the reader has any better solution, welcome to communicate and progress together ~

END

Note: If reproduced or quoted please specify the source http://www.cnblogs.com/sev7en/p/4021994.html

Self-writing SQL script query results in EF (for complex SQL logic to improve query efficiency)

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.