方法不能被翻译成店前pression [英] Method cannot be translated into a store expression

查看:119
本文介绍了方法不能被翻译成店前pression的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看到这个LINQ code工作SQL但是当我使用实体框架,它抛出这个错误:

I saw this code work with LINQ to SQL but when I use Entity Framework, it throws this error:

LINQ到实体无​​法识别方法System.Linq.IQueryable'1 [MyProject.Models.CommunityFeatures] GetCommunityFeatures()方法,而这种方法不能被翻译成店前pression.`

LINQ to Entities does not recognize the method 'System.Linq.IQueryable'1[MyProject.Models.CommunityFeatures] GetCommunityFeatures()' method, and this method cannot be translated into a store expression.`

信息库code是这样的:

The repository code is this:

public IQueryable<Models.Estate> GetEstates()
{
    return from e in entity.Estates
           let AllCommFeat = GetCommunityFeatures()
           let AllHomeFeat = GetHomeFeatures()
           select new Models.Estate
                      {
                                EstateId = e.EstateId,
                                AllHomeFeatures = new LazyList<HomeFeatures>(AllHomeFeat),
                                AllCommunityFeatures = new LazyList<CommunityFeatures>(AllCommFeat)
                      };
}

public IQueryable<Models.CommunityFeatures> GetCommunityFeatures()
{
    return from f in entity.CommunityFeatures
           select new CommunityFeatures
                      {
                          Name = f.CommunityFeature1,
                          CommunityFeatureId = f.CommunityFeatureId
                      };
}

public IQueryable<Models.HomeFeatures> GetHomeFeatures()
{
    return from f in entity.HomeFeatures
           select new HomeFeatures()
           {
               Name = f.HomeFeature1,
               HomeFeatureId = f.HomeFeatureId
           };
}

LazyList是一个扩展的IQueryable的权力清单。

LazyList is a List that extends the power of IQueryable.

有人能解释为什么这个错误发生?

Could someone explain why this error occurs?

推荐答案

原因:
按照设计,的 LINQ到实体 的要求,整个LINQ查询前pression待翻译为一个服务器查询。只有少数不相关SUBEX pressions(在不依赖于来自服务器的结果的查询前pressions)在客户端评估该查询被转换之前。没有一个已知的翻译,像GetHomeFeatures()在这种情况下,任意的方法调用,不被支持。
结果
更具体地讲,LINQ到实体唯一支持的参数构造函数的初始化的。
搜索结果
解决方案:
因此,要克服这个例外,你需要你的子查询合并到主之一的 GetCommunityFeatures()的和的 GetHomeFeatures()的而不是从LINQ查询中直接调用方法。此外,还有在你试图实例化的新实例线的问题的 LazyList 的使用它的参数化的构造,就像你可能已经在这样做的 LINQ到SQL 的。对于该解决方案将是,切换到LINQ查询的客户端评估(LINQ到对象)。这需要你调用 AsEnumerable您的LINQ 方法之前调用构造函数LazyList实体的查询。
搜索结果
像这样的东西应该工作:

Reason: By design, LINQ to Entities requires the whole LINQ query expression to be translated to a server query. Only a few uncorrelated subexpressions (expressions in the query that do not depend on the results from the server) are evaluated on the client before the query is translated. Arbitrary method invocations that do not have a known translation, like GetHomeFeatures() in this case, are not supported.
To be more specific, LINQ to Entities only support Parameterless constructors and Initializers.

Solution: Therefore, to get over this exception you need to merge your sub query into the main one for GetCommunityFeatures() and GetHomeFeatures() instead of directly invoking methods from within the LINQ query. Also, there is an issue on the lines that you were trying to instantiate a new instance of LazyList using its parameterized constructors, just as you might have been doing in LINQ to SQL. For that the solution would be to switch to client evaluation of LINQ queries (LINQ to Objects). This will require you to invoke the AsEnumerable method for your LINQ to Entities queries prior to calling the LazyList constructor.

Something like this should work:

public IQueryable<Models.Estate> GetEstates()
{
    return from e in entity.Estates.AsEnumerable()
       let AllCommFeat = from f in entity.CommunityFeatures
                         select new CommunityFeatures {
                             Name = f.CommunityFeature1,
                             CommunityFeatureId = f.CommunityFeatureId
                         },
       let AllHomeFeat = from f in entity.HomeFeatures
                         select new HomeFeatures() {
                             Name = f.HomeFeature1,
                             HomeFeatureId = f.HomeFeatureId
                         },
       select new Models.Estate {
            EstateId = e.EstateId,
            AllHomeFeatures = new LazyList<HomeFeatures>(AllHomeFeat),
            AllCommunityFeatures = new LazyList<CommunityFeatures>(AllCommFeat)
       };
}

结果
更多信息:请看一看的 LINQ到实体,不支持什么?更多信息 的。
还检查出的 LINQ到实体,解决办法是什么,不支持 的有关可能的解决方案的详细讨论。
(这两个环节是缓存版本,因为原来的网站已关闭)


More Info: Please take a look at LINQ to Entities, what is not supported? for more info. Also check out LINQ to Entities, Workarounds on what is not supported for a detailed discussion on the possible solutions. (Both links are the cached versions because the original website is down)

这篇关于方法不能被翻译成店前pression的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆