我有一个 DataTable
,如下所示:
在上面的 DT 上使用下面的 LINQ
表达式之后:
if (dt.AsEnumerable().All(row => string.IsNullOrEmpty(row.Field<string>("SameReferences"))))
BindOldReferences(dt);
else
{
var grps = from row in dt.AsEnumerable()
let RefID = row.Field<string>("ReferenceID")
let RefDescription = row.Field<string>("ReferenceDescription")
let ReferenceUrl = row.Field<string>("ReferenceUrl")
let SortOrder = row.Field<int>("sortOrder")
group row by new { RefDescription, ReferenceUrl, SortOrder } into groups
select groups;
dt = grps.Select(g =>
{
DataRow first = g.First();
if (first.Field<string>("SameReferences") != null)
{
string duplicate = first.Field<int>("SortOrder").ToString();
first.SetField("SameReferences", string.Format("{0},{1}", duplicate, first.Field<string>("SameReferences")));
}
return first;
}).CopyToDataTable();
}
将上面的 LINQ
应用到 DT 后,它变成:
预期 DT 如下:当 Samereferences
列中有单个值时,消除 (,) 逗号。那么我必须对 LINQ 进行哪些更改才能获得预期的以下输出。
请帮助...!
最佳答案
您可以使用 String.Trim像这样的方法:-
first.SetField("SameReferences", string.Format("{0},{1}", duplicate,
first.Field<string>("SameReferences")).Trim(','));
它将删除所有结尾的逗号。
关于c# - 使用 LINQ 从数据表的列中消除逗号 (,),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32111393/