LINQ to Entities 无法识别该方法
尝试执行 linq 查询时出现以下错误:
LINQ to Entities 无法识别“布尔值”方法 IsCharityMatching(System.String, System.String)' 方法,以及这个 方法无法转换为存储表达式。
我读过很多以前的问题,人们遇到了同样的错误,如果我理解正确的话,那是因为 LINQ to Entities 需要将整个 linq 查询表达式转换为服务器查询,因此您无法调用外部方法在其中。我还无法将我的场景转化为可行的东西,而且我的大脑开始崩溃,所以我希望有人能给我指出正确的方向。我们正在使用实体框架和规范模式(我对两者都是新手)。
这是使用该规范的代码:
ISpecification<Charity> specification = new CharitySearchSpecification(charityTitle, charityReference);
charities = charitiesRepository.Find(specification).OrderBy(p => p.RegisteredName).ToList();
这是 linq 表达式:
public System.Linq.Expressions.Expression<Func<Charity, bool>> IsSatisfied()
{
return p => p.IsCharityMatching(this.charityName, this.charityReference);
}
这是 IsCharityMatching 方法:
public bool IsCharityMatching(string name, string referenceNumber)
{
bool exists = true;
if (!String.IsNullOrEmpty(name))
{
if (!this.registeredName.ToLower().Contains(name.ToLower()) &&
!this.alias.ToLower().Contains(name.ToLower()) &&
!this.charityId.ToLower().Contains(name.ToLower()))
{
exists = false;
}
}
if (!String.IsNullOrEmpty(referenceNumber))
{
if (!this.charityReference.ToLower().Contains(referenceNumber.ToLower()))
{
exists = false;
}
}
return exists;
}
如果您需要更多信息,请告诉我。
非常感谢,
安妮莉
I'm getting the following error when trying to do a linq query:
LINQ to Entities does not recognize the method 'Boolean
IsCharityMatching(System.String, System.String)' method, and this
method cannot be translated into a store expression.
I've read lots of previous questions where people get the same error, and if I understand this correctly it's because LINQ to Entities requires the whole linq query expression to be translated to a server query, and therefore you can't call an outside method in it. I haven't been able to convert my scenario into something that works yet, and my brain is starting to melt down, so I was hoping someone could point me in the right direction. We're using Entity Framework and the specification pattern (and I'm new to both).
Here's the code that uses the specification:
ISpecification<Charity> specification = new CharitySearchSpecification(charityTitle, charityReference);
charities = charitiesRepository.Find(specification).OrderBy(p => p.RegisteredName).ToList();
Here's the linq expression:
public System.Linq.Expressions.Expression<Func<Charity, bool>> IsSatisfied()
{
return p => p.IsCharityMatching(this.charityName, this.charityReference);
}
Here's the IsCharityMatching method:
public bool IsCharityMatching(string name, string referenceNumber)
{
bool exists = true;
if (!String.IsNullOrEmpty(name))
{
if (!this.registeredName.ToLower().Contains(name.ToLower()) &&
!this.alias.ToLower().Contains(name.ToLower()) &&
!this.charityId.ToLower().Contains(name.ToLower()))
{
exists = false;
}
}
if (!String.IsNullOrEmpty(referenceNumber))
{
if (!this.charityReference.ToLower().Contains(referenceNumber.ToLower()))
{
exists = false;
}
}
return exists;
}
Let me know if you need any more information.
Many thanks,
Annelie
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
正如您所发现的,实体框架实际上无法将 C# 代码作为其查询的一部分运行。它必须能够将查询转换为实际的 SQL 语句。为了使其工作,您必须将查询表达式重组为实体框架可以处理的表达式。
As you've figured out, Entity Framework can't actually run your C# code as part of its query. It has to be able to convert the query to an actual SQL statement. In order for that to work, you will have to restructure your query expression into an expression that Entity Framework can handle.
我今天遇到了同样的问题,这是我点击的第一个链接。但是我并不是要验证我的查询。因此,如果其他人有同样的问题并且正在寻找此解决方案,则将其添加到此处。我的问题在另一个链接中。
这是使用实体框架并在 IQueryable 结果中转换数据以进行过滤时最常见的异常。
存在多种解决方案。将 ToString() 调用移至单独的行。
使用EF扩展方法,
在过滤之前将IQueryable结果转换为IEnumerable
I ran into the same problem today, this was the first link I hit. However I was not looking for verifying my query. So if somebody else has the same issue and are looking for this solution it is added here. My issue was in another link.
It is the most common exception occurs when working with entity framework and converting data inside IQueryable result for filtering.
Several solutions exist. Move ToString() call to a separate line.
Use EF Extension Method,
Convert IQueryable result to IEnumerable before Filtering
我在这段代码中遇到了同样的错误:
这正是我收到的错误:
我这样解决了:
我在表格后面添加了一个 .ToList() 。这将实体和 linq 代码解耦,并阻止我的下一个 linq 表达式被转换为 sql。
注意:此解决方案不是最佳解决方案,因为它会在过滤之前将整个表加载到内存中。
I got the same error in this code:
This was the exact error I received:
I solved it this way:
I added a .ToList() after my table. This decoupled the Entity and linq code and prevents my next linq expression from being translated to sql.
NOTE: this solution isn't optimal, because it loads the entire table into memory before filtering it down.
如果有人正在寻找 VB.Net 答案(就像我最初一样),这里是:
If anyone is looking for a VB.Net answer (as I was initially), here it is:
我在这段代码中遇到了同样的错误:
解决方案
IQueryable
到.toList()
是最好的选择I got the same error in this code:
Solution
IQueryable
to.toList()
is the best option