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

时间:2012-04-09 09:04:02

标签: c# asp.net linq

当我尝试运行以下代码时。

var result = from c in db.brand
             where c.title.contains("test")
             select c.title + "-" +c.brand;

List<string> lst = r.ToList();

它会出现以下错误。

  

LINQ to Entities无法识别方法&#39; System.String   的ToString()&#39;方法,这个方法无法翻译成商店   表达

3 个答案:

答案 0 :(得分:6)

我建议以匿名类型获取标题和品牌,然后在进程中执行字符串连接:

var list = db.Brand.Where(c => c.Title.Contains("test"))
                   .Select(c => new { c.Title, c.Brand })
                   .AsEnumerable() // Rest of the query in-process
                   .Select(x => x.Title + " " + x.Brand)
                   .ToList();

答案 1 :(得分:1)

试试这个:

var result = from c in db.brand where c.title.contains("test") select c;
var finalResult = result.ToList().Select(ss=> ss.title + "-" + ss.brand);

答案 2 :(得分:-1)

尝试:

var result = from c in db.brand where c.title.contains("test") select new { c.title + "-" +c.brand }