问题描述
我有以下问题,其中我想寻求帮助。我有(由一个属性条件)来选择不同的值,然后从属性的其余部分中提取不同的值。
I have the following problem, in which I would like to ask for help. I have to select distinct values(criteria by one property) and then extract the different values from the rest of the properties.
让我们说,我有下面的类,其中每个 BOM
有一个或多个 FlatComponents
不同的配件
和 QtyPer
。
Let's say that I have the following class in which each BOM
has one or more FlatComponents
with different Parts
and QtyPer
.
例如:
public sealed class BOM {
public string FlatComponent {
get;
set;
}
public string Part {
get;
set;
}
public string QtyPer {
get;
set;
}
public BOM(String flatComponent, String part, String qtyPer) {
this.FlatComponent=flatComponent;
this.Part=part;
this.QtyPer=qtyPer;
}
}
List<BOM> list=new List<BOM>();
list.Add(new BOM("a", "1", "2"));
list.Add(new BOM("a", "3", "4"));
list.Add(new BOM("b", "5", "6"));
list.Add(new BOM("c", "7", "8"));
list.Add(new BOM("c", "9", "0"));
我将如何选择不同的 FlatComponents
及其在使用LINQ(以逗号分隔),每个属性值diferent?
How would I select distinct FlatComponents
and its diferent values in each property using LINQ(separated by comma)?
Result = [
["a", "1, 3", "2, 4"],
["b", "5", "6"],
["c", "7, 9", "8, 0"]
]
我已经尝试使用 .Distinct()
,但我与LINQ很新...
I have tried using .Distinct()
, but I am pretty new with LINQ ...
推荐答案
试试这个:
var result = list
.GroupBy(e => e.FlatComponent)
.Select(e => new BOM
{
FlatComponent = e.Key,
Part = string.Join(", ", e.Select(x => x.Part)),
QtyPer = string.Join(", ", e.Select(x => x.QtyPer))
});
这将创建一个独特的 BOM
每个 FlatComponent
与<$加入部分
和 QtyPer
属性C $ C>,。
It will create a distinct BOM
for each FlatComponent
, joining the Part
and QtyPer
properties with ", "
.
这篇关于LINQ:选择从不同的LST并附加不同的属性到一个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!