NotSupportedException:实体的LINQ无法识别该方法 [英] NotSupportedException: LINQ to Entities does not recognize the method
本文介绍了NotSupportedException:实体的LINQ无法识别该方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试使此查询与EF一起使用,但会引发异常:
I'm trying to make this query work with EF but it throws an exception:
var c = ac.Communities.OrderBy(o => o.Posts.Count())
.Skip(page*limit)
.Take(limit)
.Select(o => o.ToViewModel()).ToArray();
Community
模型中的ToViewModel()
方法如下所示:
The ToViewModel()
method from the Community
model looks like this:
public CommunityModel ToViewModel()
{
return new CommunityModel()
{
category = Category.Name,
created = CreationTime,
description = Description,
id = Id,
name = Name,
ownerId = Owner.Id,
postsCount = Posts.Count(),
score = Posts.Sum(o => o.Likes - o.Unlikes),
shortDescription = ShortDescription,
subscribersCount = Subscribers.Count(),
};
}
我在做什么错了?
推荐答案
Entity Framework无法转换您在ToViewModel
内部使用的方法.使用ToList()
急于加载结果,然后映射它们,从而避免了EF尝试翻译SQL的需要:
Entity Framework cannot translate the methods you are using inside ToViewModel
. Use ToList()
to eagerly load the results, then map those instead, avoiding the need for EF to try to translate the SQL:
var c = ac.Communities.OrderBy(o => o.Posts.Count())
.Skip(page*limit)
.Take(limit)
.ToList()
.Select(o => o.ToViewModel()).ToArray();
这篇关于NotSupportedException:实体的LINQ无法识别该方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文