C# 如何合并n个列表<;字典<;字符串,字符串>&燃气轮机;使用Linq输入字典 我试图把字典的列表(动态)合并成一个给定的对象的列表,使用LINQ,我看到了许多类似于这个问题的问题,但是在它们的情况下,他们总是考虑已知的字典数量。

C# 如何合并n个列表<;字典<;字符串,字符串>&燃气轮机;使用Linq输入字典 我试图把字典的列表(动态)合并成一个给定的对象的列表,使用LINQ,我看到了许多类似于这个问题的问题,但是在它们的情况下,他们总是考虑已知的字典数量。,c#,entity-framework,linq,.net-core,C#,Entity Framework,Linq,.net Core,这是我的结构: 我有一个查询,返回如下对象列表: public class MyDto { public Dictionary<string, string> JsonDictionary { get; set; } public Guid SomeId1{ get; set; } public Guid SomeId2{ get; set; } } 如您所见,在上面的代码段中,两个对象的SomeId1和SomeId2是相同的。因此,在本例中,我想将这些对象分组,将

这是我的结构:

我有一个查询,返回如下对象列表:

public class MyDto 
{
  public Dictionary<string, string> JsonDictionary { get; set; }
  public Guid SomeId1{ get; set; }
  public Guid SomeId2{ get; set; } 
}
如您所见,在上面的代码段中,两个对象的SomeId1和SomeId2是相同的。因此,在本例中,我想将这些对象分组,将JsonContent字段合并到一个字典中

还尝试了use.GroupBy,但我无法将此jsonContent合并为一个agregion


救命啊!!!请!=)拥抱是个很快的主意,不确定这是否可以完全在linq本地完成

// lets group all of our id's and I'm not going to use groupby because I'm not a fan.
var itemsById = new Dictionary<string, List<MyDto>>();
foreach(var item in q)
{
   if(itemsById.ContainsKey(item.SomeId))
   {
       itemsById[item.SomeId].Add(item);
   }
   else 
   {
      itemsById.Add(item.SomeId, new List<MyDto>());
      itemsById[item.SomeId].Add(item);
   } 
}

so now we have dictionary of all of items by their ID.

var finalizedDtos = new List<MyDto>();
foreach(var entry in items)
{
   var finalizedDto = new MyDto{ someId = entry.Key };
   foreach(var innerDictionary in entry.value.JsonDictionary)
   {
                    var finalizedDto = new MyDto {SomeId = entry.Key};
                    var allKeyValuePairs = entry.Value.SelectMany(c => c.JsonDictionary);
                    finalizedDto.JsonDictionary = allKeyValuePairs.ToDictionary(key => key.Key, value => value.Value);
                    finalizedDtos.Add(finalizedDto);
   }
}

//让我们把所有id分组,我不会使用groupby,因为我不是一个粉丝。
var itemsbyd=新字典();
foreach(q中的var项目)
{
if(itemsById.ContainsKey(item.SomeId))
{
itemsById[item.SomeId]。添加(item);
}
其他的
{
itemsById.Add(item.SomeId,new List());
itemsById[item.SomeId]。添加(item);
} 
}
现在我们有了所有项目的ID字典。
var finalizedDtos=新列表();
foreach(项目中的var条目)
{
var finalizedDto=newmydto{someId=entry.Key};
foreach(entry.value.JsonDictionary中的var innerDictionary)
{
var finalizedDto=newmydto{SomeId=entry.Key};
var allKeyValuePairs=entry.Value.SelectMany(c=>c.JsonDictionary);
finalizedTo.JsonDictionary=allKeyValuePairs.ToDictionary(key=>key.key,value=>value.value);
finalizedDtos.Add(finalizedDto);
}
}

不是很多linq,但实际上对于嵌套结构,我想不出更好的方案了

如果你想这样做,这是可行的。。。但是,您没有提供如何处理与具有相同键但不同值的JSONDictionary的冲突。在这个场景中,我只是覆盖了以前声明的值。如果你想要不同的行为,你必须改变这一点

IEnumerable<MyDto> list = new List<MyDto>(); // This is the stuff you parsed
var results = new Dictionary<Tuple<Guid, Guid>, MyDto>();

foreach (var item in list) {
    var key = new Tuple<Guid, Guid>(item.SomeId1, item.SomeId2);
    if (results.ContainsKey(key))
        foreach (var entry in item.JsonDictionary)
            results[key].JsonDictionary[entry.Key] = entry.Value;
    else results[key] = item;
}

list = results.Values;
IEnumerable list=new list();//这就是你分析的东西
var results=newdictionary();
foreach(列表中的变量项){
var key=新元组(item.SomeId1,item.SomeId2);
if(结果容器(关键))
foreach(item.JsonDictionary中的var条目)
结果[key].JsonDictionary[entry.key]=entry.Value;
其他结果[关键]=项目;
}
列表=结果。值;
更新:

如果你真的想要的话,我用Linq写的。这是相当低效的,但我想不出很多其他方法来轻松做到这一点。如果你想提高效率,你应该使用上面的例子

var results = list
    .GroupBy(
        x => new Tuple<Guid, Guid>(x.SomeId1, x.SomeId2), 
        (x, y) => new MyDto {
                SomeId1 = x.Item1,
                SomeId2 = x.Item2,
                JsonDictionary = y
                    .SelectMany(z => z.JsonDictionary)
                    .ToLookup(z => z.Key, z => z.Value)
                    .ToDictionary(z => z.Key, z => z.First())
        });
var结果=列表
.群比(
x=>新元组(x.SomeId1,x.SomeId2),
(x,y)=>新的MyDto{
SomeId1=x.Item1,
SomeId2=x.Item2,
JsonDictionary=y
.SelectMany(z=>z.JsonDictionary)
.ToLookup(z=>z.Key,z=>z.Value)
.ToDictionary(z=>z.Key,z=>z.First())
});

Enumerable.Zip您是否尝试过Enumerable.Zip您可能可以通过覆盖哈希集并将someid定义为指定重复的键来执行类似操作,但只有在键和内容相同时才返回false,否则只需添加内容……除非ID1和ID2相同,否则无法合并。如果它们相同,你必须检查密钥是否已经存在,因为你不必在字典中输入相同的密钥。太好了@Aotn!谢谢谢谢你Jlalonde,我遇到了一些与此代码的冲突。。。但是无论如何,谢谢!!
IEnumerable<MyDto> list = new List<MyDto>(); // This is the stuff you parsed
var results = new Dictionary<Tuple<Guid, Guid>, MyDto>();

foreach (var item in list) {
    var key = new Tuple<Guid, Guid>(item.SomeId1, item.SomeId2);
    if (results.ContainsKey(key))
        foreach (var entry in item.JsonDictionary)
            results[key].JsonDictionary[entry.Key] = entry.Value;
    else results[key] = item;
}

list = results.Values;
var results = list
    .GroupBy(
        x => new Tuple<Guid, Guid>(x.SomeId1, x.SomeId2), 
        (x, y) => new MyDto {
                SomeId1 = x.Item1,
                SomeId2 = x.Item2,
                JsonDictionary = y
                    .SelectMany(z => z.JsonDictionary)
                    .ToLookup(z => z.Key, z => z.Value)
                    .ToDictionary(z => z.Key, z => z.First())
        });