Mutilevel include in C# Linq

妖精的绣舞 提交于 2019-12-13 13:20:54

问题


I want to have MULTILEVEL include in my linq statment, something like

var a = departments.include(u=>u.Customers)
                   .include(u=>u.Customers.Include(u=>u.Orders);

How should i do that?

Thanks


回答1:


You of course can use lambda expression but you must use special format:

var a = departments.Include(d => d.Customers.Select(c => c.Orders));



回答2:


This should do the trick:

departments.Include("Customers.Orders");

Obviously you can't use a lambda expression anymore.

cf. documentation here: http://msdn.microsoft.com/en-us/library/bb738708.aspx




回答3:


[Jeroenh was quicker, but lambda is ok i think...]

Shouldnt be a problem. For entity framework without proxies we use:

var a=departments.Include("Customers.Orders").Single(dept => dept.Customers.Id == id);

/Victor




回答4:


You could specify which nested objects to be retrieved when the query is being executed by using the LoadWith method.



来源:https://stackoverflow.com/questions/8742581/mutilevel-include-in-c-sharp-linq

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!