【问题标题】:Create a datatable on the basis of a column of another Datatable Groupby using linq?使用linq基于另一个Datatable Groupby的列创建数据表?
【发布时间】:2018-02-20 04:27:46
【问题描述】:

我有一个数据表,其中的数据是这样的,其中一个特征可以有多个 Storyid 和不同的人分配给它。第一张图片的第 4 行 featureid 是 26450 而不是 26540)我的错误


我想要另一个数据表,其中相同的功能将讲述故事 id 和 hrs(sum) 并分配给图片中显示的内容

【问题讨论】:

  • 第一个表中的最后两行是否应该与Featureid 具有相同的值? (4和5调换)
  • 你有什么尝试吗?
  • @StephenMuecke 可以不同(所有行都可以不同)
  • 我想你误会了。第 3 行的值为 26450,第 4 行的值为 26540 - 但您希望随后被分组到第二个表中。这只是一个错字吗?它们的值是否应该都是25450
  • 不是错字,我希望他们根据功能 ID 分组

标签: c# linq datatable


【解决方案1】:

myTable 成为您当前的表,然后您可以尝试使用以下代码来获得您正在查找的结果:

var result = myTable.AsEnumerable().GroupBy(x=> x["Featureid"])
                                   .Select(item => new 
                                   {
                                       Feature = item.Key, 
                                       AssignedTo = string.Join(",", item.Select(a=>a["AssignedTo"])),
                                       Stories = string.Join(",", item.Select(s=>s["storyid"])),
                                       CompletedHrs= item.Sum(y=>y["CompletedHrs"]))
                                    });

【讨论】:

  • 我想你的意思是item.Select(s=>s.storyid)
  • 获取错误 Featureid 在当前上下文中不存在
  • @AkhilJain:我做了一些更新,请检查一下
  • 您能告诉我如何将查询数据(结果)放入数据表吗? @un-lucky
  • @AkhilJain:试试看:result.CopyToDataTable<DataRow>();
【解决方案2】:

让你的表名是FeatureAssigned

,那么你可以试试下面的代码。它会给你预期的输出

var result = db.FeatureAssigned
      // If you have a filter add the .Where() here ...
      .GroupBy(e => e.Featureid)
      .ToList()
      // Because the ToList(), this select projection is not done in the DB
      .Select(eg => new 
       {
          Feature= eg.Key,
          AssignedTo = string.Join(",", eg.Select(b=>b.AssignedTo )),
          Stories = string.Join(",", eg.Select(c=>c.storyid)),
          CompletedHrs= eg.Sum(z=>z.CompletedHrs)
       });

【讨论】:

  • 这与15分钟前添加的答案相同!
  • 什么是分贝? @法尔汉
猜你喜欢
  • 1970-01-01
  • 2020-10-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-04-09
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多