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)