我是否可以根据字符搜索将数据表上的一列拆分为多列

本文关键字:一列 拆分 数据表 是否 搜索 字符 | 更新日期: 2023-09-27 18:36:36

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

举个例子

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

我希望这像

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

这可能吗?

我是否可以根据字符搜索将数据表上的一列拆分为多列

缺少一些信息。

假设 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)
});