var q = from c in db.Customer.Include("SalesOrderHeader") .Include("SalesOrderHeader.SalesOrderDetail") .Include("SalesOrderHeader.SalesOrderDetail.Product") where c.CustomerID == 514 select c;
var q = from c
in
db.Customer.Include(
"SalesOrderHeader"
) .Include(
"SalesOrderHeader.SalesOrderDetail"
"SalesOrderHeader.SalesOrderDetail.Product"
) where c.CustomerID == 514 select c;
var q = from c in db.Customer.Include("SalesOrderHeader.SalesOrderDetail.Product") where c.CustomerID == 514 select c;
↑ Return to Top
Question: I want to retrieve an object from ObjectContext without going to the database server. How can I execute queries locally rather than at the database?
Answer: Entity Framework queries are executed at the database rather than locally. The following blog shows how to write an extension method to return results based on what is in ObjectContext: http://blogs.msdn.com/b/dsimmons/archive/2009/02/21/local-queries.aspx.
Answer: All filtering performed before results are materialized is performed on the database server. Any filtering performed after materializing results (via ToList, ToArray etc.) will occur on the client.
Question: How can I see the T-SQL that is generated when I execute my queries?
Answer: You can use ObjectQuery's ToTraceString. For more information, see http://social.msdn.microsoft.com/Forums/en-US/adodotnetentityframework/thread/4a17b992-05ca-4e3b-9910-0018e7cc9c8c. Note that it is not recommended to use ToTraceString for anything other than debugging purposes. If you are using SqlClient to connect to the database then T-SQL is also displayed in the Visual Studio Intelli-Trace window.
The following articles discuss how to work with pre-compiled queries: http://msdn.microsoft.com/en-us/magazine/ee336024.aspx%20http://msdn.microsoft.com/en-us/library/bb896297.aspx http://blogs.msdn.com/b/dmcat/archive/2010/03/08/potential-performance-issues-with-compiled-linq-query-re-compiles.aspx http://thedatafarm.com/blog/data-access/using-pre-compiled-linq-to-entities-queries-in-web-apps-and-services/ http://blogs.msdn.com/b/dsimmons/archive/2010/01/12/ef-merge-options-and-compiled-queries.aspx
Question: What are the different ways and considerations for loading related objects? Will using Load rather than Include improve performance?
Answer: In general, LINQ to Entities will load from the database whatever information the query asks for in the outermost projection. It may appear that Include could be specified in many different locations in a query. The code will compile as long as you place Include anywhere ObjectQuery is available. However, at run time, Include makes sense only in certain cases:
Most times, if any of the conditions above is not fulfilled, the span information specified in Include will be ignored. While a query of the following form will usually work:
var query = from entity
context.MyEntitySet.Include(
"NavProperty"
)
where entity.ConditionProp ==
someValue
select entity
In general, it is often clearer to express it like this:
context.MyEntitySet
select entity; var queryWithSpan =
((ObjectQuery)query).Include(
);
var query = query.Include("NavProperty");
var query = query.Include(
var process = context.Processes .Where("it.ProcessId = @id", new ObjectParameter("id",pId));
var process = context.Processes .Where(
"it.ProcessId = @id"
,
new
ObjectParameter(
"id"
,pId));
Question: When working with Entity SQL, it is strongly recommended to use parameterized queries with ObjectParameters or EntityParameters to avoid SQL injection attacks. What about injection attacks in LINQ to Entities queries? How do I create parameterized LINQ queries?
Answer: Unlike Entity SQL queries, LINQ to Entities queries are not composed by using string manipulation or concatenation, and they are not susceptible to common SQL injection attacks. For example, the following LINQ to Entities query will translate to a parameterized T-SQL query:
var query = from person in context.People where person.LastName == LastName && person.FirstName == FirstName select person;
var query = from person
context.People where person.LastName == LastName && person.FirstName == FirstName select person;
Answer: No. You can project into an anonymous class or a defined class that is not EntityType. If StudentGrade is EntityType, an exception will be thrown when you execute the following query:
var query = from g in context.StudentGrades select new StudentGrade { EnrollmentID = g.EnrollmentID, Grade = g.Grade };
var query = from g
context.StudentGrades select
StudentGrade { EnrollmentID = g.EnrollmentID, Grade = g.Grade };
Question: Where can I find information about using the Defining Query element within the SSDL of the Entity Framework, advantages of using it, and scenarios where it would be helpful?
Answer: The following blog discusses defining query: http://blogs.microsoft.co.il/blogs/gilf/archive/2008/05/14/how-why-use-definingquery-element.aspx
Answer: In EF4, support was added for Contains method. Prior to EF4, you need to work around the fact that it is missing. Several suggestions may be found in this stack overflow thread.
Carsten Siemens edited Revision 19. Comment: Fixed misspelling and added tag: LINQ
Carsten Siemens edited Revision 18. Comment: Added link to TechNet Wiki article "How to Query Trees Using LINQ"
Carsten Siemens edited Revision 17. Comment: Fixed a TOC problem
Carsten Siemens edited Revision 16. Comment: Reformatted and reorganized the whole article. Add top level chapte for "General LINQ Questions" and
Ed Price - MSFT edited Revision 13. Comment: Cleaning up the TOC
ulcer edited Revision 12. Comment: Added table of contents
Revision 16. & 17. Comment: Reformatted and reorganized the whole article to make it more readable. Added top level chapters for "General LINQ Questions" and "Entity Framework Related LINQ Questions".