gpt4 book ai didi

c# - 动态linq嵌套组

转载 作者:行者123 更新时间:2023-11-30 23:07:22 25 4
gpt4 key购买 nike

如何做动态嵌套组?

假设我有这个例子:

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

我如何更改它以按参数动态分组。从更深的地方开始,然后继续。

最佳答案

如果你想真正动态地嵌套它,我不会将关卡名称作为属性存储在你的关卡对象中,而是将它们放在一个数组中,这样你就可以以某种方式循环遍历它们。

话虽如此,你可以建立一个通用的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)));
}

关于c# - 动态linq嵌套组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47377869/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com