LINQ - 转换为字符串LIST并替换字符

时间:2013-02-26 20:18:15

标签: c# linq

我有一个从存储过程返回的数据集,我将其保存到字符串LIST中。

到目前为止,这就是我所拥有的并且它正常工作:

 this.UserOrgs = ds.Tables[0].AsEnumerable()
           .Select(r => r.Field<string>(0))
                            .ToList();

但是,在将它们放入列表之前,我还需要在返回的值中将所有“X”替换为“Y”。

我想我的问题是,我可以使用LINQ一次完成所有操作吗?或者,在将其转换为列表之前,是否应该替换数据集中的字符?

2 个答案:

答案 0 :(得分:6)

当然,这很容易做到 - 你甚至不需要另外一个电话:

this.UserOrgs = ds.Tables[0].AsEnumerable()
                            .Select(r => r.Field<string>(0).Replace("X", "Y"))
                            .ToList();

答案 1 :(得分:0)

var chars = new[] { "X", "Y" };
EnumerableRowCollection<string> enumerableRowCollection = dataTable.AsEnumerable().Select(r => chars.Aggregate(r.Field<string>(0), (s, s1) => s.Replace(s1, string.Empty)));

上述答案将取代所有X和所有Y.

至少你放在char数组中的任何东西