带有子聚合的 LINQ 聚合
使用半复杂结构,我尝试使用 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
我尝试使用 Distinct
和 Aggregate
但它只是没有得到我在任何地方。我总是没有得到一份清单或另一份清单。谁能帮忙解决这个问题吗?
更新
这是我当前聚合的示例。
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 sameAspect.Name
, and get a complete list ofSteps
, 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果步长和光圈之间没有相关性,您可以这样做:
如果有,您需要以某种方式将两者结合起来。如果台阶索引到孔径中,那么您可以使用类似的东西。
If there's no correlation between steps and apertures you can do this:
If there is you'll need to combine the two somehow. If steps index into apertures then you can use something similar.
更新后:
这将创建如您的示例中所示的输出。
现在,如何将其与当前的聚合方法合并?据我了解,您想从所有当前布局内容(包括排列、页面等)创建一个大布局吗?
您根本不需要聚合,只需将其分成 3 个 LINQ 查询即可:
After your updates:
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:
我认为这不是您想要的完整解决方案:
其中缺少什么?您还想如何“聚合”安排和步骤? (编辑:阅读您的评论后,也许这实际上就是您想要的。)
I assume this isn't the complete solution that you want:
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.)