试图串联LINQ中的列 [英] Trying to concatenate columns in LINQ
问题描述
我正在尝试将2列连接成1列用于结果集.
I'm trying to concatenate 2 columns into 1 column for my resultset.
var gridInfo = from leader in db.SchoolLdrAdminAccesses
join emp in db.Employees
on leader.ID equals emp.ID
select new List<string> { leader.ID, string.Format("{0}{1}", emp.FirstName, emp.LastName) };
我遇到此错误:
LINQ to Entities无法识别方法'System.String Format(System.String,System.Object,System.Object)'方法,以及此方法 方法不能转换为商店表达式.
LINQ to Entities does not recognize the method 'System.String Format(System.String, System.Object, System.Object)' method, and this method cannot be translated into a store expression.
我做错了什么?
我必须保留这个:
select new List<string> {...}
当我这样做时,我需要在List结构中使用它.
As I am then doing this and I need it in the List structure.
return Json(new
{
sEcho = param.sEcho,
iTotalRecords = gridInfo.Count(),
iTotalDisplayRecords = gridInfo.Count(),
aaData = gridInfo.ToArray()
},
JsonRequestBehavior.AllowGet);
推荐答案
使用+
运算符在EF中连接两个字符串:
Use the +
operator to concat two strings in EF:
select new List<string> { leader.ID, emp.FirstName + emp.LastName };
与string.Format
不同,查询提供程序知道如何将+
运算符转换为SQL.
Unlike string.Format
, the query provider knows how to translate the +
operator into SQL.
为此,请执行相同的操作以连接到您拥有的任何其他C#代码中的字符串.使用string.Format
仅连接两个字符串是不必要的复杂操作,从而使读者感到困惑.
And for that matter, do the same thing to concat to strings in any other C# code that you have. Using string.Format
to just concat two strings is needlessly complex, thus confusing readers.
这篇关于试图串联LINQ中的列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!