相关疑难解决方法(0)

LINQ to Entities无法识别方法'System.String ToString()'方法,并且此方法无法转换为存储表达式

我正在将一些东西从一个mysql服务器迁移到一个sql server但我无法弄清楚如何使这个代码工作:

using (var context = new Context())
{
    ...

    foreach (var item in collection)
    {
        IQueryable<entity> pages = from p in context.pages
                                   where  p.Serial == item.Key.ToString()
                                   select p;
        foreach (var page in pages)
        {
            DataManager.AddPageToDocument(page, item.Value);
        }
    }

    Console.WriteLine("Done!");
    Console.Read();
}
Run Code Online (Sandbox Code Playgroud)

当它进入第二个时foreach (var page in pages)它抛出一个异常说:

LINQ to Entities无法识别方法'System.String ToString()'方法,并且此方法无法转换为存储表达式.

谁知道为什么会这样?

c# mysql sql linq

126
推荐指数
7
解决办法
18万
查看次数

LINQ to Entities无法识别该方法

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

LINQ to Entities无法识别方法'Boolean 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();
Run Code Online (Sandbox Code Playgroud)

这是linq表达式:

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

这是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;
}
Run Code Online (Sandbox Code Playgroud)

如果您需要更多信息,请与我们联系.

非常感谢,

Annelie

.net linq linq-to-entities entity-framework specification-pattern

111
推荐指数
1
解决办法
11万
查看次数

17
推荐指数
2
解决办法
1万
查看次数