使用Linq修剪DataTable单元格中的空白 [英] Trim whitespace from DataTable cells with Linq

查看:88
本文介绍了使用Linq修剪DataTable单元格中的空白的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这段代码用于修剪每个数据行的每个数据单元中的所有空间. 我如何获得此代码:

This piece of code works to trim all spaces in each datacell of each datarow. How can I get this code:

var dataRows = dataTable.AsEnumerable();
foreach (var row in dataRows)
{
     var cellList = row.ItemArray.ToList();
     row.ItemArray = cellList.Select(x => x.ToString().Trim()).ToArray();
}

插入一行代码,这样我就不必遍历每一行?像这样的东西,但是不起作用:

into one line of code so I don't have to loop through each row? Something like this but it doesn't work:

dataTable.AsEnumerable().Select(y => y.ItemArray.ToList()).Select(x => x.ToString().Trim());

推荐答案

如果您喜欢LINQish stype:

If you love LINQish stype:

  dataTable.AsEnumerable().ToList()
        .ForEach(row =>
        {
            var cellList = row.ItemArray.ToList();
            row.ItemArray = cellList.Select(x => x.ToString().Trim()).ToArray();
        });

这篇关于使用Linq修剪DataTable单元格中的空白的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆