LINQ to Entities不识别方法'System.Linq.IQueryable [英] LINQ to Entities does not recognize the method 'System.Linq.IQueryable
问题描述
IGrpTextRepository rep = new GrpTextRepository( );
var query = new DetailViewModel
{
ViewDet =(from gh in _db.Grp
select new MultiDetailViewModel
{
Header = gh ,
Txts = rep.FindAllLangTxtById(gh.GrpID)
})ToList(),
Lang = _db.Language.ToList(),
};
我的界面是
public interface IGrpTextRepository
{
IQueryable&GrpText> FindAllLangTxtById(int GrpID);
}
public class GrpTextRepository:IGrpTextRepository
{
DBEntities db = new DBEntities();
public IQueryable&GrpText> FindAllLangTxtById(int GrpID)
{
返回(从lang在db.Language
中将lang.LangID中的db.GrpText中的gtxts连接到gtxts.LangID等于jointxt
from fintxt in jointxt .DefaultIfEmpty()
其中fintxt.GrpID == GrpID
select fintxt);
}
}
完整的错误消息
System.NotSupportedException:LINQ to Entities不识别方法'System.Linq.IQueryable`1 [aaa.Models.GrpText] FindAllLangTxtById(Int32)'方法,这样方法不能转换为存储表达式。
简单的解决方案是在选择()之前添加一个.ToList()。 ..)。
ViewDet = _db.Grp.ToList()。选择(gh => new MultiDetailViewModel ...)
这样,初始请求将转到数据库,返回结果,然后可以使用select对于Linq的陈述对象。
但是我必须问,为什么组和文本之间没有FK关系,因此两者之间的实体关联允许你只需访问 gh.GrpText
并获取文本的集合(或使用.Include())加载的文本(或热切加载)。
由于@Doguhan Uluca在评论中指出,使用 ToList()
是有风险的,因为它会导致该表中的所有内容被提取到内存中。你只应该在非常小的集合上使用它。正确的方法是修复数据库设计,以便您有效地查询。
When I try to call my repository in a Sub Select, I got this error.
IGrpTextRepository rep = new GrpTextRepository();
var query = new DetailViewModel
{
ViewDet = (from gh in _db.Grp
select new MultiDetailViewModel
{
Header = gh,
Txts = rep.FindAllLangTxtById(gh.GrpID)
}).ToList(),
Lang = _db.Language.ToList(),
};
My Interface is
public interface IGrpTextRepository
{
IQueryable<GrpText> FindAllLangTxtById(int GrpID);
}
public class GrpTextRepository : IGrpTextRepository
{
DBEntities db = new DBEntities();
public IQueryable<GrpText> FindAllLangTxtById(int GrpID)
{
return (from lang in db.Language
join gtxts in db.GrpText on lang.LangID equals gtxts.LangID into jointxt
from fintxt in jointxt.DefaultIfEmpty()
where fintxt.GrpID == GrpID
select fintxt);
}
}
Here is the full Error Message
System.NotSupportedException: LINQ to Entities does not recognize the method 'System.Linq.IQueryable`1[aaa.Models.GrpText] FindAllLangTxtById(Int32)' method, and this method cannot be translated into a store expression.
The simple solution is to add a .ToList() before your select(...).
ViewDet = _db.Grp.ToList().Select(gh => new MultiDetailViewModel ...)
That way the initial request will go to the database, come back with the results and you can then reproject them using a select statement against Linq to objects.
But I have to ask why there isn't an FK relationship between the groups and the texts and thus an Entity Association between the two allowing you to just access gh.GrpText
and get to the collection of texts lazily loaded (or eagerly loaded using .Include()).
As @Doguhan Uluca points out in the comments, using ToList()
is risky as it will cause everything in that table to be fetched into memory. You should only use it on very small collections. The correct approach is to fix your database design so you can query it efficiently.
这篇关于LINQ to Entities不识别方法'System.Linq.IQueryable的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!