从多个字典中获取不同的键

Tro*_*ics 2 c# dictionary

我有本具有相同类型键的字典。我需要为所有三个字典获取不同的键。我怎样才能做到这一点?

var rmDict = rmTrxs
  .GroupBy(x => new { x.Name, x.Pfx.Id })
  .ToDictionary(z => z.Key, z => z.ToList());

var vaDict = vaTrxs
  .GroupBy(x => new { x.Name, x.Pfx.Id })
  .ToDictionary(z => z.Key, z => z.ToList());

var smDict = smTrxs
  .GroupBy(x => new { x.Name, x.Pfx.Id })
  .ToDictionary(z => z.Key, z => z.ToList());
Run Code Online (Sandbox Code Playgroud)

现在我需要从rmDict,vaDict和 中获取不同的键smDict

Dmi*_*nko 5

我理解你是对的,你可以使用Concat所有的键,然后在以下帮助下摆脱重复项Distinct

 using System.Linq;

 ...

 var distinctKeys = rmDict
   .Keys
   .Concat(vaDict.Keys)
   .Concat(smDict.Keys)
   .Distinct();
Run Code Online (Sandbox Code Playgroud)

对于非Linq解决方案,您可以使用HashSet<T>

 //TODO: put the right type instead of MyType
 var distinctKeys = new HashSet<MyType>(rmDict.Keys);

 distinctKeys.UnionWith(vaDict.Keys);
 distinctKeys.UnionWith(smDict.Keys);
 
Run Code Online (Sandbox Code Playgroud)