无法将方法转换为商店表达式 [英] Can't translate method into a store expression
问题描述
与我发现的问题最接近的是这一个据我所知,这与同一问题无关.我可能会弄错了,在这种情况下,我希望有人能解释相似性的发展过程(除了相同的词指代错误).话虽如此,我有以下问题.
The closest to my questions that I've found is this one and as far I can see, it's not regarding the same issue. I might be mistaken, in which case, I hope someone can explain how the similarity goes (except for the same words referring to the error, that is). With that being said, I have the following issue.
这按预期工作.
List<Typo> things = context.Things.Where(thing => thing.Nice).ToList();
但是,不是.
List<Typo> things = context.Things.Where(thing => IsNice(thing)).ToList();
...
private bool IsNice(Typo thing) { return thing.Nice; }
上下文的类型为 ModelContainer ,派生 DbContext .有人告诉我这是EF的标准设置,我没有理由怀疑.该错误消息要求以下内容.
context is of type ModelContainer deriving DbContext. I've been told that it's a standard setup for EF and I have no reason to suspect otherwise. The error message claims the following.
{"LINQ to Entities无法识别XXX方法,并且该方法无法转换为商店表达式."}
{"LINQ to Entities does not recognize the method XXX method, and this method cannot be translated into a store expression."}
我没有这个错误的经验,坦率地说,我所做的研究几乎没有使我明白.我可以研究一下(a)以我的方式使它工作,并且(b)对其进行进一步调查.
I have no experience with this error and, frankly, the research I've made gave me very little clarity. What can I look into to (a) make it work my way and (b) investigate it further.
推荐答案
IMO,这仍然是您提到的问题:LINQ to Entities无法解释IsNice
,因此,您需要普通" LINQ来解决Where方法.您可以使用AsEnumberable
方法强制执行此操作:
IMO, it's still the same issue as the question you mention: LINQ to Entities cannot interpret IsNice
, and thus, you need 'normal' LINQ to resolve the Where method. You can force this with the AsEnumberable
method:
List<Typo> things = context.Things.AsEnumerable().Where(thing => IsNice(thing)).ToList();
或更短.
List<Typo> things = context.Things.Where(IsNice).ToList();
这篇关于无法将方法转换为商店表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!