LINQ聚合与次级总 [英] LINQ Aggregate with Sub-Aggregates

查看:94
本文介绍了LINQ聚合与次级总的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

采用半结构复杂,我想'结合'的几个对象到使用LINQ的总结办法(不过,如果有更好的办法,我就是其中之一开放的想法)。

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; }
}



基本上,我试图聚集的<$ C的整个层次$ C>的IEnumerable<安排及GT; ,并保持在基本水平的一切,但那里的东西可以适当覆盖,我要覆盖它们。

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.

我想获得的所有安排共享同一 Aspect.Name ,并获得步骤的完整列表,覆盖较低的水平,其中的步骤更高层次安排有不同的要求值相同等级。

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.

所以就拿...

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?

下面是我当前的聚集的例子。

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())

    });
        }



我的问题是,我有很多的麻烦周围环绕我的头如何做到这一点在所有,一个表达要少得多。我不是不愿意使用多种方法,但如果我可以封装这一切,那将是非常有用的。我对LINQ一般迷住了,我真的想找到自己的解决这个头。

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.

其他的收集,孔径,会以类似的方式工作,但它是毫无关系的步骤。根本就是两个不同的阵列我必须做同样的事情,但他们没有任何共同点彼此。 。学习如何与一个做到这会给我的知识与其他做

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.

推荐答案

您更新后:

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.

现在,如何与当前聚合方法合并呢?至于我understanging,你想从所有当前布局的内容(包括安排,页等)创建一个大的布局?

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)?

您不必总可言,只是把它拆分成3 LINQ查询:

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
       };

这篇关于LINQ聚合与次级总的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆