相关疑难解决方法(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万
查看次数

LINQ to Entity,使用SQL LIKE运算符

我有一个LINQ to ENTITY查询从表中提取,但我需要能够创建一个"模糊"类型的搜索.所以我需要添加一个where子句,按照姓氏搜索,如果他们在搜索框中添加条件(文本框,可以为空白---在这种情况下,它会拉动所有内容).

这是我到目前为止:

    var query = from mem in context.Member
                orderby mem.LastName, mem.FirstName
                select new
                {
                    FirstName = mem.FirstName,
                    LastName = mem.LastName,

                };
Run Code Online (Sandbox Code Playgroud)

这将把所有内容从Entity对象中的Member表中拉出来.

然后我对逻辑有了补充:

sLastName = formCollection["FuzzyLastName"].ToString();

if (!String.IsNullOrEmpty(sLastName))
   query = query.Where(ln => ln.LastName.Contains(sLastName));
Run Code Online (Sandbox Code Playgroud)

问题是当按下搜索按钮时,不返回任何内容(0结果).我已经运行了我期望在这里发生的SQL Server查询,它返回6个结果.

这是我期望的查询:

SELECT mem.LastName, mem.FirstName FROM Members mem WHERE mem.LastName LIKE '%xxx%'
Run Code Online (Sandbox Code Playgroud)

(当xxx输入文本框时)

有人看到这个有什么不对吗?

编辑:修复了SELECT查询.我的意思是它读取LIKE'%xxx%'(NOT ='xxx")

asp.net-mvc linq-to-entities entity-framework

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