LINQ to Entities 无法识别该方法 [英] LINQ to Entities does not recognize the method

查看:26
本文介绍了LINQ to Entities 无法识别该方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试执行 linq 查询时出现以下错误:

I'm getting the following error when trying to do a linq query:

LINQ to Entities 无法识别方法 'BooleanIsCharityMatching(System.String, System.String)' 方法,以及这个方法无法转换为存储表达式.

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.

我已经阅读了很多以前的问题,人们遇到了同样的错误,如果我理解正确,那是因为 LINQ to Entities 需要将整个 linq 查询表达式转换为服务器查询,因此您不能调用其中的一个外部方法.我还没有能够将我的场景转换成可行的东西,我的大脑开始崩溃,所以我希望有人能指出我正确的方向.我们正在使用实体框架和规范模式(我对两者都不熟悉).

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

这是使用规范的代码:

ISpecification<Charity> specification = new CharitySearchSpecification(charityTitle, charityReference);

charities = charitiesRepository.Find(specification).OrderBy(p => p.RegisteredName).ToList();

这是 linq 表达式:

Here's the linq expression:

public System.Linq.Expressions.Expression<Func<Charity, bool>> IsSatisfied()
{
    return p => p.IsCharityMatching(this.charityName, this.charityReference);
}

这是 IsCharityMatching 方法:

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.

非常感谢,

安妮莉

推荐答案

正如您所发现的,Entity Framework 实际上不能将您的 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.

public System.Linq.Expressions.Expression<Func<Charity, bool>> IsSatisfied()
{
    string name = this.charityName;
    string referenceNumber = this.referenceNumber;
    return p => 
        (string.IsNullOrEmpty(name) || 
            p.registeredName.ToLower().Contains(name.ToLower()) ||
            p.alias.ToLower().Contains(name.ToLower()) ||
            p.charityId.ToLower().Contains(name.ToLower())) &&
        (string.IsNullOrEmpty(referenceNumber) ||
            p.charityReference.ToLower().Contains(referenceNumber.ToLower()));
}

这篇关于LINQ to Entities 无法识别该方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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