加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 使用LINQ从数据表的列中删除逗号(,)

发布时间:2020-12-15 22:18:50 所属栏目:百科 来源:网络整理
导读:我有一个DataTable,如下所示: 在上面的DT上使用LINQ Expression之后: if (dt.AsEnumerable().All(row = string.IsNullOrEmpty(row.Fieldstring("SameReferences")))) BindOldReferences(dt);else{ var grps = from row in dt.AsEnumerable() let RefID = r
我有一个DataTable,如下所示:

enter image description here

在上面的DT上使用LINQ Expression之后:

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后,它变为:

enter image description here

预期的DT如下:当列Samereferences中存在单个值时消除(,)逗号.那么我必须对LINQ做出哪些改变以获得预期的低于输出.

enter image description here

请帮忙..!

解决方法

您可以像这样使用 String.Trim方法: –

first.SetField("SameReferences",first.Field<string>("SameReferences")).Trim(','));

它将删除所有尾随逗号.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读