2

嗨,我想做的是在 C# 上编写代码,将我的数据表中的特定列拆分为 2 列,如下所示

举个例子

Name   Location
-----  --------
ross    a12u20
charle  b12u25

我希望这就像

Name   Location   Unit
-----  --------   -----
ross    a12        u20
charle  b12        u25

这可能吗?

4

1 回答 1

0

缺少一些信息。

假设 Location 属性总是 6 个字符长,或者单位从索引 4 开始,这可能是一个快速而肮脏的解决方案:

List<person> source = new List<person>();
//I have created a ficitive class person with 2 properties: Name, Location
source.Add(new person { Name ="Ross", Location="a12u15"});
source.Add(new person { Name ="bald", Location="b12u20"});
source.Add(new person { Name ="fat", Location="c12u25"});
source.Add(new person { Name ="man", Location="d12u30"});
source.Add(new person { Name ="heinz", Location="e12u35"});


var result = source.Select(s => new {
    Name = s.Name,
    Location = s.Location.Substring(0,3),
    Unit = s.Location.Substring(3,3)
});

编辑:要从数据表中读取,查询看起来会有所不同。source将是您的数据表。

var result = source.Select(s => new {
    Name = s.Field<string>("Name"),
    Location = s.Field<string>("Location").Substring(0,3),
    Unit = s.Field<string>("Location").Substring(3,3)
});
于 2013-10-24T07:00:01.350 回答