带有子聚合的 LINQ 聚合

发布于 2024-11-03 13:24:02 字数 2440 浏览 0 评论 0原文

使用半复杂结构,我尝试使用 Linq Aggregate 方法将多个对象“组合”为一个对象(尽管如果有更好的方法,我愿意接受想法)。

这是我的基本课程设计。

class Aspect {
  string Name { get; set; }
}
class Arrangement {
  Aspect Aspect { get; set; }
  IList<Aperture> Apertures { get; set; }
  IList<Step> Steps { get; set; }
}

class Step {
  int Rank { get; set; }
  int Required { get; set; }
}
class Aperture {
  string Name { get; set; }
  int Size { get; set; }
}

基本上,我试图聚合 IEnumerable 的整个层次结构,并将所有内容保留在基本级别上,但是在可以适当覆盖的地方,我想覆盖它们。

更新

我想获取共享相同Aspect.Name的所有Arrangements,并获取Steps的完整列表,覆盖较低级别的Steps其中更高级别的安排具有相同的排名,但需要的值不同。

举例来说......

var list = new List<Arrangement>{
    new Arrangement{
      Aspect = Aspects.Named("One"),
      Steps = new List<Step>{
            new Step {
               Rank = 1,
               Required = 2
            },
            new Step {
               Rank = 2,
               Required = 4
            }
        }
    },
    new Arrangement{
      Aspect = Aspects.Named("One"),
      Steps = new List<Step>{
            new Step {
               Rank = 1,
               Required = 3
            }
        }
    }
  }

当正确聚合时,它应该看起来像......

Arrangement
 - Aspect
    - Name : One
 - Steps 
    - Rank : 1
    - Required : 3
    - Rank : 2
    - Required : 4

我尝试使用 DistinctAggregate 但它只是没有得到我在任何地方。我总是没有得到一份清单或另一份清单。谁能帮忙解决这个问题吗?

更新

这是我当前聚合的示例。

public static Layouts.Template Aggregate(this IList<Layouts.Template> source) {
            return source.Aggregate(
                source.First(),
                (current, next) => new Layouts.Template {
                    Apertures = (current.Apertures.Concat(next.Apertures).Distinct().ToList()),
                    Arrangements = (current.Arrangements.Concat(next.Arrangements).Distinct().ToList()),
                    Pages = (current.Pages.Concat(next.Pages).Distinct().ToList())

    });
        }

我的问题是,我很难弄清楚如何做到这一点,更不用说用一种表达方式了。我并不是不愿意使用多种方法,但如果我能将它们全部封装起来,那将非常有用。总的来说,我对 LINQ 很着迷,并且我真的很想解决这个问题。

更新 2

另一个集合 Apertures 将以类似的方式工作,但它与 Steps 无关。我必须对两个不同的数组执行相同的操作,但它们彼此之间没有任何共同点。学习如何与一个人一起做这件事将使我掌握与另一个人一起做这件事的知识。

Using a semi-complex structure, I am trying to 'combine' several objects into one using the Linq Aggregate method (though if there is a better way, I am open to ideas).

Here is my basic class design.

class Aspect {
  string Name { get; set; }
}
class Arrangement {
  Aspect Aspect { get; set; }
  IList<Aperture> Apertures { get; set; }
  IList<Step> Steps { get; set; }
}

class Step {
  int Rank { get; set; }
  int Required { get; set; }
}
class Aperture {
  string Name { get; set; }
  int Size { get; set; }
}

Basically, I am trying to aggregate the entire hierarchy of an IEnumerable<Arrangement> and keep everything on the base level, but where things can appropriately overwrite, I want to overwrite them.

Update

I want to get all Arrangements that share the same Aspect.Name, and get a complete list of Steps, overwriting lower level Steps where higher level Arrangements have the same Rank with a different Required value.

So take for instance...

var list = new List<Arrangement>{
    new Arrangement{
      Aspect = Aspects.Named("One"),
      Steps = new List<Step>{
            new Step {
               Rank = 1,
               Required = 2
            },
            new Step {
               Rank = 2,
               Required = 4
            }
        }
    },
    new Arrangement{
      Aspect = Aspects.Named("One"),
      Steps = new List<Step>{
            new Step {
               Rank = 1,
               Required = 3
            }
        }
    }
  }

When aggregated properly, it should come out to look like ...

Arrangement
 - Aspect
    - Name : One
 - Steps 
    - Rank : 1
    - Required : 3
    - Rank : 2
    - Required : 4

I have attempted to use Distinct and Aggregate and it just isn't getting me anywhere. I keep ending up not getting one list or the other. Can anyone help with this?

Update

Here is an example of my current aggregation.

public static Layouts.Template Aggregate(this IList<Layouts.Template> source) {
            return source.Aggregate(
                source.First(),
                (current, next) => new Layouts.Template {
                    Apertures = (current.Apertures.Concat(next.Apertures).Distinct().ToList()),
                    Arrangements = (current.Arrangements.Concat(next.Arrangements).Distinct().ToList()),
                    Pages = (current.Pages.Concat(next.Pages).Distinct().ToList())

    });
        }

My problem is that I'm having a lot of trouble wrapping my head around how to do this at all, much less in one expression. I'm not unwilling to use multiple methods, but if I could encapsulate it all, it would be really useful. I am fascinated by LINQ in general and I really want to get my head around this.

Update 2

The other collection, Apertures, will work in a similar manner, but it is unrelated to the Steps. Simply two different arrays I must do the same thing to, but they have nothing in common with one another. Learning how to do this with one will give me the knowledge to do it with the other.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

摇划花蜜的午后 2024-11-10 13:24:02

如果步长和光圈之间没有相关性,您可以这样做:

var result = new Arrangement{
    Steps = list.SelectMany(arrangement => arrangment.Steps)
                .GroupBy(step => step.Rank)
                .Select(l => l.Last())
                .OrderBy(step => step.Rank)
                .ToList()),
}

如果有,您需要以某种方式将两者结合起来。如果台阶索引到孔径中,那么您可以使用类似的东西。

If there's no correlation between steps and apertures you can do this:

var result = new Arrangement{
    Steps = list.SelectMany(arrangement => arrangment.Steps)
                .GroupBy(step => step.Rank)
                .Select(l => l.Last())
                .OrderBy(step => step.Rank)
                .ToList()),
}

If there is you'll need to combine the two somehow. If steps index into apertures then you can use something similar.

呆橘 2024-11-10 13:24:02

更新后:

var query = arrangements
    .GroupBy(a => a.Aspect.Name)
    .Select(g => 
    new Arrangement
    { 
        Steps = ga.SelectMany(a => a.Steps)
                  .GroupBy(s => s.Rank)
                  .Select(gs => gs.Last()),
        Aspect = ga.First().Aspect
    });

这将创建如您的示例中所示的输出。

现在,如何将其与当前的聚合方法合并?据我了解,您想从所有当前布局内容(包括排列、页面等)创建一个大布局吗?

您根本不需要聚合,只需将其分成 3 个 LINQ 查询即可:

// get all arrangements object from all layouts [flattening with SelectMany]
var arrangements = source.SelectMany(s => s.Arrangements);
// and filter them
var filteredArrangements = // enter larger query from above here         

// repeat the same for Apertures and Pages

... 

// and return single object
return new Layouts.Template 
       {
           Apertures = filteredApertures,
           Arrangements = filteredArrangements,
           Pages = filteredPages
       };

After your updates:

var query = arrangements
    .GroupBy(a => a.Aspect.Name)
    .Select(g => 
    new Arrangement
    { 
        Steps = ga.SelectMany(a => a.Steps)
                  .GroupBy(s => s.Rank)
                  .Select(gs => gs.Last()),
        Aspect = ga.First().Aspect
    });

This will create output as in your example.

Now, how to merge it with your current aggregation method? As to my understanging, you want to create one big layout from all current layout contents (including arrangements, pages, etc)?

You don't need aggregate at all, just split it into 3 LINQ queries:

// get all arrangements object from all layouts [flattening with SelectMany]
var arrangements = source.SelectMany(s => s.Arrangements);
// and filter them
var filteredArrangements = // enter larger query from above here         

// repeat the same for Apertures and Pages

... 

// and return single object
return new Layouts.Template 
       {
           Apertures = filteredApertures,
           Arrangements = filteredArrangements,
           Pages = filteredPages
       };
回忆追雨的时光 2024-11-10 13:24:02

我认为这不是您想要的完整解决方案:

private static Arrangement Accumulate(IEnumerable<Arrangement> arrangements)
{
    var stepsByRank = new Dictionary<int, Step>();

    foreach (var arrn in arrangements)
        foreach (var step in arrn.Steps)
            stepsByRank[step.Rank] = step;

    return new Arrangement { Steps = stepsByRank.Values.ToArray() };
}

其中缺少什么?您还想如何“聚合”安排和步骤? (编辑:阅读您的评论后,也许这实际上就是您想要的。)

I assume this isn't the complete solution that you want:

private static Arrangement Accumulate(IEnumerable<Arrangement> arrangements)
{
    var stepsByRank = new Dictionary<int, Step>();

    foreach (var arrn in arrangements)
        foreach (var step in arrn.Steps)
            stepsByRank[step.Rank] = step;

    return new Arrangement { Steps = stepsByRank.Values.ToArray() };
}

What's missing from this? How else do you want to "aggregate" the arrangements and steps? (edit: after reading your comment, maybe this actually is what you want.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文