Linq-to-Entities包含方法未找到 [英] Linq-to-Entities Include method not found
问题描述
var wg = from _ in _repo.Workgroups.Include(Contact)。ToList() ;
但是我收到以下错误:
'System.Linq.IQueryable'不包含'Include'的定义,并且没有扩展方法'Include'接受类型'System.Linq.IQueryable'的第一个参数可以找到(你是缺少使用指令或程序集引用?)
尽管这种方法内置于EF4中。我有没有启用它?
是的方法是建立在EF中,但不能在 IQueryable
界面。它可以在 ObjectQuery
中找到。如果你想在 IQueryable
上调用它,你必须创建自己的扩展,将当前的查询转换为 ObjectQuery
并执行包含
。如下所示:
public static IQueryable< T>包含< T>(此IQueryable< T>查询,字符串属性)
{
var objectQuery = Query as ObjectQuery< T>;
if(objectQuery == null)
{
抛出新的NotSupportedException(Include只能在ObjectQuery上调用);
}
return objectQuery.Include(property);
}
或者你必须使用实体框架功能CTP5 ,其中此类扩展已经可用。
I am using EF4 within a MVC3 application and I was looking for a way to view all my contacts within a given workgroup. In the controller I specified:
var wg = from w in _repo.Workgroups.Include("Contact").ToList();
but I get the following error:
'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 (are you missing a using directive or an assembly reference?)
I though this method was built into EF4. Do I have enable it somehow?
Yes the method is build into EF but it is not available on IQueryable
interface. It is available on ObjectQuery
. If you want to call it on IQueryable
you must create your own extension wich will convert current query to ObjectQuery
and perform Include
. Something like:
public static IQueryable<T> Include<T>(this IQueryable<T> query, string property)
{
var objectQuery = query as ObjectQuery<T>;
if (objectQuery == null)
{
throw new NotSupportedException("Include can be called only on ObjectQuery");
}
return objectQuery.Include(property);
}
Or you must use Entity Framework Feature CTP5 where such extensions are already available.
这篇关于Linq-to-Entities包含方法未找到的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!