【问题标题】:Reforming Table/Entity using SQL or Linq to Object使用 SQL 或 Linq to Object 重构表/实体
【发布时间】:2021-12-13 14:08:14
【问题描述】:

我正在尝试将包含名称列名称的表 A 规范化,其中名称由“/”连接并分隔到表 B 中,其中名称被拆分为行,状态被复制到新行中。我用谷歌搜索并尝试了没有成功,请帮忙。

表-A

Name State
Ken/Terri/Roberto New York
David/Ken Chicago
Kevin/John/Mary New Jersey

表-B

Name State
Ken New York
Terri New York
Roberto New York
David Chicago
Ken Chicago
Kevin New Jersey
John New Jersey
Mary New Jersey

【问题讨论】:

  • 你需要说的不仅仅是“我用谷歌搜索并尝试过”。另外,你为什么要标记 [linq]?

标签: sql linq


【解决方案1】:

您可以使用SelectMany()Select()Split() 的组合

输入

var tableA = new []
{
    new {Name = "Ken/Terri/Roberto", State = "New York"},
    new {Name = "David/Ken", State = "New York"},
    new {Name = "Kevin/John/Mary", State = "New York"}
};

Linq

var result = tableA.SelectMany(x =>   //Flatten the given sequence
       x.Name.Split('/')              //Split each name by '/'
      .Select(y => new {Name = y, State = x.State}));  //Create Anonymous object for each name

Try it Online

【讨论】:

  • @Hussein 回答你的问题了吗?
  • 太棒了,完美运行,谢谢
猜你喜欢
  • 2010-12-17
  • 1970-01-01
  • 1970-01-01
  • 2023-03-11
  • 1970-01-01
  • 2011-04-27
  • 1970-01-01
  • 2011-03-25
  • 2010-09-05
相关资源
最近更新 更多