【问题标题】:LINQ merge two grouped query resultsLINQ 合并两个分组的查询结果
【发布时间】:2023-04-09 19:31:01
【问题描述】:

在我的域中,我有一个名为“Block”的对象。 “块”可以附加“引脚”和“属性”。

我可以创建两个组,得到以下结果:

组 1 是:

Block:B1
    Pin:P1
    Pin:P2
Block:B2
    Pin:P3
    Pin:P4

第 2 组是:

Block:B1
    Attribute:Att1, Value: Value1
    Attribute:Att2, Value: Value2
Block:B2
    Attribute:Att3, Value: Value3
    Attribute:Att4, Value: Value4

现在我想把这两个结果合并成这样的:

结果组是:

Block:B1
    Pin:P1
    Pin:P2
    Attribute:Att1, Value: Value1
    Attribute:Att2, Value: Value2
Block:B2
    Pin:P3
    Pin:P4
    Attribute:Att3, Value: Value3
    Attribute:Att4, Value: Value4

使用 LINQ 可以吗?或者我以后需要未分组的结果来创建分组结果吗?

谢谢

编辑: 使用 Road242 解决方案,我得到以下结果:

Pin: P1
Pin: P2
Attribute: Att1
Attribute: Att2
Pin: P3
Pin: P4
Attribute: Att3
Attribute: Att4

引脚和属性看起来不错,但我把块作为分组键丢失了:-(

编辑: 我的查询如下所示:

var blockPinsQuery = from blockPinRelation in blockPinRelations
            join block in blocks
                on blockPinRelation.BlockId equals block.Id
            join pin in pins
                on blockPinRelation.PinId equals pin.Id
            group pin by block
            into g
            select new
                   {
                       Block = g.Key,
                       Pins = g
                   };

var blockAttributesQuery = from blockAttributeRelation in blockAttributeRelations
                             join block in blocks
                                 on blockAttributeRelation.BlockId equals block.Id
                             join attribute in stringAttributes
                                 on blockAttributeRelation.AttributeId equals attribute.Id
                                   group attribute by block
                                 into g
                                 select new
                                 {
                                     Block = g.Key,
                                     Attributes = g
                                 };

如您所见,相关实体之间有一个关系表。但这并不重要,我猜。

我的输出例程如下所示:

foreach (var group in blockPinsQuery)
        {
            Console.WriteLine("Block:" + group.Block.Name);
            group.Pins.ToList().ForEach(x => Console.WriteLine("\t" + "Pin:" + x.Name));
        }

foreach (var group in blockAttributesQuery)
        {
            Console.WriteLine("Block:" + group.Block.Name);
            group.Attributes.ToList().ForEach(x => Console.WriteLine("\t" + "Attribute:" + x.Name + ", " + "Value: " + x.Value));
        }

对于 Road242 查询:

foreach (var group in joinedResults)
        {
            group.Pins.ToList().ForEach(p => Console.WriteLine("Pin: " + p.Name));
            group.Attributes.ToList().ForEach(p => Console.WriteLine("Attribute: " + p.Name));
        }

【问题讨论】:

  • 能否请您发布 Block 的声明,以便查看它的属性等。谢谢
  • 使用带有强类型返回的Union,而不是匿名..
  • @André Figueiredo:我试图使用 Union()。正如您提到的 Union() 需要一个强类型的返回值,但我不知道如何实现这一点。如何使用强类型返回值?

标签: c# linq


【解决方案1】:

你可以使用 linq join 子句:

http://msdn.microsoft.com/en-us/library/bb311040.aspx

var joinedResults =
    from g1 in Group1
    join g2 in Group2 on g1.Block equals g2.Block
    select new { Pin = g1.Pin, Attribute = g2.Attribute };

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-05
    • 1970-01-01
    • 2016-10-27
    • 1970-01-01
    相关资源
    最近更新 更多