LINQ 表达式语法如何与 Include() 一起用于预先加载
我在下面有一个查询,但我想执行 Include() 来急切加载属性.Actions 有一个导航属性,User(Action.User)
I have a query below, but I want to perform an Include() to eager load properties. Actions has a navigation property, User (Action.User)
1) 我的基本查询:
from a in Actions
join u in Users on a.UserId equals u.UserId
select a
2) 第一次尝试:
from a in Actions.Include("User")
join u in Users on a.UserId equals u.UserId
select a
但 Action.User 未填充.
But Action.User is not populated.
3) 尝试在查询之外的操作中急切地将用户"加载到导航属性中:
3) Try to eager load 'User' into the navigation property in action outside of query:
(from a in Actions
join u in Users on a.UserId equals u.UserId
select a).Include("User")
在尝试包含的 LINQPad 中,我收到一个错误:
In LINQPad trying Include's I get an error:
System.Linq.IQueryable"不包含Include"的定义,并且找不到接受System.Linq.IQueryable"类型的第一个参数的扩展方法Include"(按 F4 添加 using 指令或汇编参考)
'System.Linq.IQueryable' does not contain a definition for 'Include' and no extension method 'Include' accepting a first argument of type 'System.Linq.IQueryable' could be found (press F4 to add a using directive or assembly reference)
我认为这是因为 LINQ 不支持 Include().
I think this is because LINQ doesn't support Include().
所以我在VS中尝试过;查询 2 运行,但返回未填充的用户属性.Query 3 的扩展方法似乎不存在,尽管它确实存在于没有查询的 Action 本身.
So I tried in VS; query 2 runs, but returns unpopulated User property. Query 3 the extension method does not seem to exist, although it does exist on Action itself without the query.
我想通了,无论如何感谢您的建议.解决方案是这样做(我的问题中的第二次尝试):
I figured it out, thanks for the suggestions anyway. The solution is to do this (2nd attempt in my question):
var qry = (from a in Actions
join u in Users on a.UserId equals u.UserId
select a).Include("User")
intellisense 在查询后没有显示 Include 的原因是因为我需要使用以下内容:
The reason intellisense didn't show Include after the query was because I needed the following using:
using System.Data.Entity;
这样做一切正常.