[英]Convert List<List<List<T>>> into List<List<T>> based on key
I have a this complex 3D structure of List<List<List<myClass>>>
and I want it to reduced to List<List<myClass>>
. 我有
List<List<List<myClass>>>
这样复杂的3D结构,我希望将其简化为List<List<myClass>>
。
public class MyClass
{
public string Key { get; set; }
public decimal Price { get; set; }
}
The problem is that the MyClass
is been duplicated in the origin 3D lists with the same key like: 问题是
MyClass
是在原始3D列表中使用相同的键重复的,例如:
(I'm having trouble to write 3D and 2D multi-dimensional array so I just hard coded an example.) (我在编写3D和2D多维数组时遇到麻烦,因此我仅对示例进行了硬编码。)
List<List<List<MyClass>>> level1 = new List<List<List<MyClass>>>();
List<List<MyClass>> level2_1 = new List<List<MyClass>>();
List<MyClass> level3_1 = new List<MyClass>()
{
new MyClass() { Key = "key1", Price = 10 }
new MyClass() { Key = "key2", Price = 20 }
};
level2_1.Add(level3_1);
List<List<MyClass>> level2_2 = new List<List<MyClass>>();
List<MyClass> level3_2 = new List<MyClass>()
{
new MyClass() { Key = "key2", Price = 10 }
new MyClass() { Key = "key3", Price = 20 }
};
level2_2.Add(level3_2);
I need the converted list will be like: 我需要转换后的列表将是这样的:
List<List<MyClass>> level1 = new List<List<MyClass>>();
List<MyClass> level2_1 = new List<MyClass>()
{
new MyClass() { Key = "key1", Price = 10 }
}
List<MyClass> level2_2 = new List<MyClass>()
{
new MyClass() { Key = "key2", Price = 10 },
new MyClass() { Key = "key2", Price = 20 }
}
List<MyClass> level2_3 = new List<MyClass>()
{
new MyClass() { Key = "key3", Price = 20 }
}
level1.Add(level2_1);
level1.Add(level2_2);
level1.Add(level2_3);
So the main list is distinct by the Key
and the child list to be duplicated by its Price
s. 因此,主列表由
Key
区分,子列表由Price
重复。
Note that Iv'e looked through these question: 1 , 2 , 3 , 需要注意的是Iv'e通过这些问题看: 1 , 2 , 3 ,
Any other elegant way achieving this? 还有其他优雅的方法可以做到这一点吗? maybe linq?
也许linq?
Try select many 尝试选择许多
public static List<List<MyClass>> MyConvertLinq(List<List<List<MyClass>>> items)
{
var allItems = items.SelectMany(m => m).ToList();
return allItems;
}
--- Edit --- -编辑-
you can use a GroupBy to build Groups 您可以使用GroupBy建立群组
public static List<List<MyClass>> MyConvertLinq(List<List<List<MyClass>>> items)
{
var allItems = items.SelectMany(m => m).ToList().SelectMany(m => m).ToList();
var sortedItems = allItems.GroupBy(m => m.Key, m => m,
(k, classes) => classes.ToList()).ToList();
return sortedItems;
}
The best solution I was able to think of: 我能想到的最佳解决方案:
public List<List<MyClass>> MyConvert(List<List<List<MyClass>>> items)
{
Dictionary<string, List<MyClass>> resultsDic = new Dictionary<string, List<MyClass>>();
foreach (List<List<MyClass>> item in items)
{
foreach (List<MyClass> innerItem in item)
{
foreach (MyClass myClass in innerItem)
{
if (!resultsDic.ContainsKey(myClass.Key))
{
resultsDic.Add(myClass.Key, innerItem);
}
}
}
}
List<List<MyClass>> convertedResults = resultsDic.Select(x => x.Value).ToList();
return convertedResults;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.