繁体   English   中英

LINQ分组并选择属性

[英]LINQ grouping and selecting into properties

我仍在尝试摆脱LINQ的困扰,并为此遇到了麻烦。 我有一个List<Data>像这样:

GroupId|SectionId|Content
-------------------------
      1|        3|part1-2
      1|        7|part1-1
      3|        3|part3-2
      1|        1|part1-3
      3|        1|part3-3
      3|        7|part3-1

我想将其转换为IEnumerable<AnonymousType> ,其中每个AnonymousType代表一个GroupId并且每个属性都链接到特定的SectionId 换句话说,您最终将得到如下结果:

{
  {
    GroupId = 1,
    Part1 = "part1-1", // From SectionId == 7
    Part2 = "part1-2", // From SectionId == 3
    Part3 = "part1-3", // From SectionId == 1
  },
  {
    GroupId = 3,
    Part1 = "part3-1", // From SectionId == 7
    Part2 = "part3-2", // From SectionId == 3
    Part3 = "part3-3", // From SectionId == 1
  }
}

像这样:

 from r in something
 group r by r.GroupId into g
 select new {
      GroupId = g.Key,
      Part1 = g.First(r => r.SectionId == 7).Content,
      Part2 = g.First(r => r.SectionId == 3).Content,
      Part3 = g.First(r => r.SectionId == 1).Content
 }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM