如何进行动态嵌套组?
让我说这个例子:
result.GroupBy(f => f.level1)
.Select(l1 => new {
name = l1.Key,
children = l1.GroupBy(l2 => l2.level2)
.Select(l2 => new {
name = l2.Key,
children = l2.GroupBy(l3 => l3.level3)
.Select(l3 => new {
name = l3.Key,
children = new someObject()
});
如何通过参数动态更改此组。 从更深层开始继续。
答案 0 :(得分:2)
如果你想真正动态地嵌套它,我不会将关卡名称存储为你的关卡对象中的属性,而是将它们放在一个数组中,这样你就可以以某种方式循环它们。
话虽如此,你可以构建一个通用的NestedGroupBy<>
Linq扩展方法来处理你想要做的事情,你只需要为你想要的每个级别传递一个lambda:
// The NestedGroupBy<> extension method
public static class LinqExtensions
{
public static IEnumerable<TTarget> NestedGroupBy<TSource, TTarget, TKey>(this IEnumerable<TSource> source, Func<TKey, IEnumerable<TTarget>, TTarget> factory, params Func<TSource, TKey>[] keySelectors)
{
return source.NestedGroupBy(factory, keySelectors, 0);
}
private static IEnumerable<TTarget> NestedGroupBy<TSource, TTarget, TKey>(this IEnumerable<TSource> source, Func<TKey, IEnumerable<TTarget>, TTarget> factory, Func<TSource, TKey>[] keySelectors, int selectorIndex)
{
// reached the end, just return an empty list
if(selectorIndex >= keySelectors.Length)
{
return new List<TTarget>();
}
// do the GroupBy using the function at index selectorIndex in our list to find the key (level name)
// then call the factory to construct the target SomeObject, passing it the key and the recursive call to NestedGroupBy<>
return source.GroupBy(keySelectors[selectorIndex])
.Select(f => factory(
f.Key,
f.NestedGroupBy(factory, keySelectors, selectorIndex + 1)
)
);
}
}
// source object - assuming your result variable is List<LevelObject>
public class LevelObject
{
public string level1 {get;set;}
public string level2 {get;set;}
public string level3 {get;set;}
public LevelObject(string level1, string level2, string level3)
{
this.level1 = level1;
this.level2 = level2;
this.level3 = level3;
}
}
// target object - what we will end up with in our final list
// the constructor is optional - it just makes the NestedGroupBy<> call cleaner.
public class SomeObject
{
public string name {get; set;}
public IEnumerable<SomeObject> children {get; set;}
public SomeObject(string name, IEnumerable<SomeObject> children)
{
this.name = name;
this.children = children;
}
}
// Sample code to use it. The JToken/JsonConvert call at the end just pretty prints the result on screen.
public static void Main()
{
List<LevelObject> result = new List<LevelObject>()
{
new LevelObject("L1a1", "L2a1", "L3a1"),
new LevelObject("L1a1", "L2a2", "L3a1"),
new LevelObject("L1a1", "L2a1", "L3a2"),
new LevelObject("L1b1", "L2b1", "L3b1"),
new LevelObject("L1c1", "L2c1", "L3c1")
};
/* old way - produces same result
var groupings = result.GroupBy(f => f.level1)
.Select(l1 => new SomeObject {
name = l1.Key,
children = l1.GroupBy(l2 => l2.level2)
.Select(l2 => new SomeObject{
name = l2.Key,
children = l2.GroupBy(l3 => l3.level3)
.Select(l3 => new SomeObject{
name = l3.Key,
children = new List<SomeObject>()
})})}).ToList();
*/
var groupings = result.NestedGroupBy<LevelObject, SomeObject, string>(
(key, children) => new SomeObject(key, children),
l => l.level1, l => l.level2, l => l.level3
).ToList();
Console.WriteLine(groupings.GetType());
Console.WriteLine(JToken.Parse(JsonConvert.SerializeObject(groupings)));
}