方法无法转换为 store 表达式 [英] Method cannot be translated into a store expression

查看:28
本文介绍了方法无法转换为 store 表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看到这段代码适用于 LINQ to SQL,但是当我使用实体框架时,它抛出了这个错误:

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

LINQ to Entities 无法识别方法 'System.Linq.IQueryable'1[MyProject.Models.CommunityFeatures] GetCommunityFeatures()' 方法,并且此方法无法转换为存储表达式.`

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.`

存储库代码是这样的:

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 是一个 List,它扩展了 IQueryable 的功能.

LazyList is a List that extends the power of IQueryable.

谁能解释为什么会出现这个错误?

Could someone explain why this error occurs?

推荐答案

原因:按照设计,LINQ to Entities 需要要转换为服务器查询的整个 LINQ 查询表达式.在转换查询之前,在客户端上只计算几个不相关的子表达式(查询中的表达式,不依赖于来自服务器的结果).不支持没有已知翻译的任意方法调用,如本例中的 GetHomeFeatures().
更具体地说,LINQ to Entities 仅支持无参数构造函数初始化器.

解决方案:因此,要克服此异常,您需要将子查询合并到 GetCommunityFeatures()GetHomeFeatures() 的主查询中,而不是直接从 LINQ 查询中调用方法.此外,您尝试使用参数化构造函数实例化 LazyList 的新实例时存在问题,就像您在 LINQ to SQL 中所做的那样.为此,解决方案是切换到 LINQ 查询的客户端评估(LINQ to Objects).这将要求您调用 AsEnumerable调用 LazyList 构造函数之前的 LINQ to Entities 查询方法.

这样的事情应该可以工作:

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 to Entities,不支持什么? 了解更多信息.另请查看 LINQ to Entities,不支持的解决方法 详细讨论可能的解决方案.(两个链接都是缓存版本,因为原网站宕机了)


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)

这篇关于方法无法转换为 store 表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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