LINQ到实体不能识别方法 [英] LINQ to Entities does not recognize the method

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

问题描述

我收到试图做一个LINQ查询时出现以下错误:

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

LINQ到实体不能识别方法布尔   IsCharityMatching(System.String,System.String)的方法,而这   方法不能被翻译成店前pression。

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.

我读过很多previous问题,人们得到了同样的错误,如果我理解这个正确的,那是因为LINQ到实体,需要全LINQ查询EX pression翻译到服务器的查询,因此,你不能够在它调用外部方法。我一直没能转换我的情况下能够正确地运行着呢,我的大脑已经开始融化,所以我希望有人能指出我在正确的方向。我们正在使用实体框架和规范模式(我是新来的这两种)。

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

下面是一个使用规范中的code:

Here's the code that uses the specification:

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

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

这里的LINQ EX pression:

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.

非常感谢,

Annelie

推荐答案

当你想通了,实体框架不能真正运行你的C#code作为查询的一部分。它必须能够将查询转换为实际的SQL语句。为了这一目标的工作,你将不得不调整您的查询EX pression到前pression的实体框架可以处理。

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到实体不能识别方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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