C# 多级字典中的嵌套foreach到LINQ

C# 多级字典中的嵌套foreach到LINQ,c#,linq,C#,Linq,我想使用LINQ简化下面嵌套的foreach循环,但无法找到方法。我想我可以使用SelectMany和lambda,但不确定。我想在这个嵌套迭代之后创建ClassA对象的列表。感谢您的帮助: public List<ClassA> GetLists(Dictionary<string, Dictionary<IEnumerable, Dictionary<string, ClassB>>> groups) { var retOutput =

我想使用LINQ简化下面嵌套的foreach循环,但无法找到方法。我想我可以使用SelectMany和lambda,但不确定。我想在这个嵌套迭代之后创建ClassA对象的列表。感谢您的帮助:

public List<ClassA> GetLists(Dictionary<string, Dictionary<IEnumerable, Dictionary<string, ClassB>>> groups)
{
    var retOutput = new List<ClassA>();

    foreach (KeyValuePair<string, Dictionary<IEnumerable, Dictionary<string, ClassB>>> group1 in groups)
    {
        foreach (KeyValuePair<IEnumerable, Dictionary<string, ClassB>> group2 in group1.Value)
        {
            foreach (KeyValuePair<string, ClassB> group3 in group2.Value)
            {
                GetList(retOutput, group1.Key, 
                    group2.Key, 
                    group3);
            }
        }
    }

    return retOutput;
}

private static void GetList(List<ClassA> retOutput, 
    string group1Key, 
    IEnumerable group2Key, 
    KeyValuePair<string, ClassB> group3)
{
    List<List<string>> itemIdsLists = group3.Value.ItemId.IntoChunks(2000);
    foreach (var itemIdList in itemIdsLists)
    {
        var currentRequest = new ClassA
        {
            TransactionType = group1Key,
            Filters = new Dictionary<string, object>(),
            ItemIds = new List<string>(),
            PropStreamsDict = new Dictionary<string, Tuple<long, string>>()
        };
        if (group2Key is Dictionary<string, object>)
        {
            currentRequest.Filters = (Dictionary<string, object>)group2Key;
        }
        currentRequest.PropStreamsDict.Add(group3.Key, Tuple.Create(group3.Value.StreamId,
            group3.Value.Uom));
        currentRequest.ItemIds.AddRange(itemIdList);
        retOutput.Add(currentRequest);
    }
}
public List getlist(字典组)
{
var retOutput=新列表();
foreach(组中的KeyValuePair组1)
{
foreach(组1.Value中的KeyValuePair组2)
{
foreach(组2.Value中的KeyValuePair组3)
{
GetList(retOutput,group1.Key,
第2组:关键,
第3组);
}
}
}
返回输出;
}
私有静态void GetList(List retOutput,
字符串group1Key,
IEnumerable group2Key,
KeyValuePair组3)
{
List itemIdsLists=group3.Value.ItemId.IntoChunks(2000);
foreach(ItemIdLists中的var itemIdList)
{
var currentRequest=新类别A
{
TransactionType=group1Key,
过滤器=新字典(),
ItemId=新列表(),
PropStreamsDict=新词典()
};
if(group2Key是字典)
{
currentRequest.Filters=(字典)group2Key;
}
currentRequest.PropStreamsDict.Add(group3.Key,Tuple.Create)(group3.Value.StreamId,
组3.价值(计量单位);
currentRequest.ItemIds.AddRange(itemIdList);
retOutput.Add(currentRequest);
}
}

您应该使用
选择many
来嵌套
foreach

以下是我的想法:

public List<ClassA> GetLists(Dictionary<string, Dictionary<IEnumerable, Dictionary<string, ClassB>>> groups)
{
    return groups
        .SelectMany(grp1 => grp1.Value
            .SelectMany(grp2 => grp2.Value
                .SelectMany(grp3 => grp3.Value.ItemId
                    .IntoChunks(2000)
                    .Select(itemIdList =>
                        new ClassA
                        {
                            TransactionType = grp1.Key,
                            Filters = grp2.Key is Dictionary<string, object> ? 
                                (Dictionary<string, object>)grp2.Key :
                                new Dictionary<string, object>(),
                            ItemIds = new List<string>(itemIdList),
                            PropStreamsDict = new Dictionary<string, Tuple<long, string>>
                            {
                                { grp3.Key, Tuple.Create(grp3.Value.StreamId, grp3.Value.Uom) }
                            }
                        }
                    )
                )
            )
        )
        .ToList();
}
public List getlist(字典组)
{
返回组
.SelectMany(grp1=>grp1.Value
.SelectMany(grp2=>grp2.Value
.SelectMany(grp3=>grp3.Value.ItemId
.IntoChunks(2000年)
.Select(itemIdList=>
新甲级
{
TransactionType=grp1.Key,
Filters=grp2。键是字典?
(字典)grp2.Key:
新字典(),
ItemId=新列表(itemIdList),
PropStreamsDict=新词典
{
{grp3.Key,Tuple.Create(grp3.Value.StreamId,grp3.Value.Uom)}
}
}
)
)
)
)
.ToList();
}

你没有发布你的
ClassA
ClassB
,所以我不得不猜测。

我真的不明白这个问题,你的问题是如何使用LinQ来处理对象?问题是……有没有更好的方法(可能是使用LinQ)来避免第一种方法中的这些嵌套foreach循环?哦,是的,我想是的,你可以使用LinQ简化很多方法,当然