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

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

问题描述

我看到这个代码与LINQ to SQL一起工作,但是当我使用Entity Framework时,它会抛出以下错误:


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


存储库代码是这样的:

  public IQueryable< Models.Estate> GetEstates()
{
从entity.Estates中的e返回
let AllCommFeat = GetCommunityFeatures()
let AllHomeFeat = GetHomeFeatures()
选择新Models.Estate
{
EstateId = e.EstateId,
AllHomeFeatures = new LazyList< HomeFeatures>(AllHomeFeat),
AllCommunityFeatures = new LazyList< CommunityFeatures>(AllCommFeat)
};
}

public IQueryable&Models.CommunityFeatures> GetCommunityFeatures()
{
从f中的f返回entity.CommunityFeatures
选择新的社区功能
{
名称= f.CommunityFeature1,
CommunityFeatureId = f.CommunityFeatureId
};
}

public IQueryable< Models.HomeFeatures> GetHomeFeatures()
{
从entity.HomeFeatures中的f返回
选择新的HomeFeatures()
{
Name = f.HomeFeature1,
HomeFeatureId = f .HomeFeatureId
};
}

LazyList是扩展IQueryable功能的列表。



有人可以解释为什么会出现此错误?

解决方案

原因: strong>
根据设计, LINQ to Entities 需要将整个LINQ查询表达式转换为服务器查询。在查询翻译之前,客户端将仅评估少数不相关的子表达式(查询中不依赖于服务器结果的表达式)。在这种情况下,GetHomeFeatures()不具有已知翻译的任意方法调用不受支持。


更具体地说,LINQ to Entities仅支持无参数构造函数和初始化程序



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



这样的东西应该可以工作:

  public IQueryable< Models.Estate> ; GetEstates()
{
从entity.Estates.AsEnumerable()中的e返回
让AllCommFeat =从实体中的f。 f.CommunityFeature1,
CommunityFeatureId = f.CommunityFeatureId
},
let AllHomeFeat = from f in entity.HomeFeatures
选择新的HomeFeatures(){
Name = f。 HomeFeature1,
HomeFeatureId = f.HomeFeatureId
},
选择新Models.Estate {
EstateId = e.EstateId,
AllHomeFeatures = new LazyList< HomeFeatures>(AllHomeFeat ),
AllCommunityFeatures = new LazyList< CommunityFeatures>(AllCommFeat)
};
}



更多信息:请查看 LINQ to Entities,什么不支持? 了解更多信息。
另外查看 LINQ to Entities,关于什么不支持的解决方法 有关可能的解决方案的详细讨论。
(两个链接都是缓存版本,因为原始网站已关闭)


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

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

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 is a List that extends the power of IQueryable.

Could someone explain why this error occurs?

解决方案

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)
       };
}


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)

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

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