Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 连接或合并两个字典,其中相同的键产生联合值_C#_Linq_Dictionary_Enums - Fatal编程技术网

C# 连接或合并两个字典,其中相同的键产生联合值

C# 连接或合并两个字典,其中相同的键产生联合值,c#,linq,dictionary,enums,C#,Linq,Dictionary,Enums,我正在使用这个链接的答案 以产生我想要的结果。不幸的是,我不知道如何将结果转换回dictionary对象,因为结果是某种类型的System.Linq.Enumerable+Where SelectEnumerableIterator,我不知道该怎么处理它 好的,细节。 假设我有以下几点: [Flags] enum People { Adam = 0x1, Barry = 0x2, Chris = 0x4, David = 0x8, Eric = 0x10, Frank

我正在使用这个链接的答案

以产生我想要的结果。不幸的是,我不知道如何将结果转换回dictionary对象,因为结果是某种类型的System.Linq.Enumerable+Where SelectEnumerableIterator,我不知道该怎么处理它

好的,细节。 假设我有以下几点:

[Flags]
enum People { Adam = 0x1, Barry = 0x2, Chris = 0x4, David = 0x8, 
            Eric = 0x10, Frank = 0x20, George = 0x40, Harold = 0x80 };

Dictionary<string, People> EUInterest = new Dictionary<string, People>() { 
    { "athletes", People.Adam | People.Barry }, 
    { "artists", People.Frank | People.Harold } };

Dictionary<string, People> USInterest = new Dictionary<string, People>() { 
    { "athletes", People.Chris | People.Harold }, 
    { "artists", People.Eric } };

var result = from interest in EUInterest.Keys
             where USInterest.ContainsKey(interest)
             let v1 = EUInterest[interest]
             let v2 = USInterest[interest]
             select new Dictionary<string, People>() { {interest, v1 | v2 } };
[标志]
枚举人{Adam=0x1,Barry=0x2,Chris=0x4,David=0x8,
Eric=0x10,Frank=0x20,George=0x40,Harold=0x80};
Dictionary EUInterest=新字典(){
{“运动员”,人们,亚当,人们,巴里},
{“艺术家”,人物。弗兰克|人物。哈罗德};
Dictionary USInterest=新建Dictionary(){
{“运动员”,人们,克里斯|人们,哈罗德},
{“艺术家”,人物。埃里克};
var结果=来自EUInterest.Keys中的利息
其中USInterest.ContainsKey(利息)
设v1=EUInterest[利息]
设v2=USInterest[兴趣]
选择new Dictionary(){{interest,v1 | v2};
当我在调试器中查看结果时,我看到“结果视图”几乎是我想要的。看起来它用一个键创建了两个字典。现在我仔细看了这个查询,它就是这么做的

以下是我想要的结果:

Dictionary<string, People> result =  new Dictionary<string, People>() { 
    { "athletes", People.Adam | People.Barry | People.Chris | People.Harold }, 
    { "artists", People.Frank | People.Harold | People.Eric } };
Dictionary result=new Dictionary(){
{“运动员”,人们,亚当,巴里,克里斯,哈罗德,
{“艺术家”,人物。弗兰克|人物。哈罗德|人物。埃里克};

即使我能够从select查询中获得所需的
var结果
,我也不确定如何访问该数据,因为它不是Dictionary对象。(我可以很容易地使用Andrew Orsich在这里提供的美妙的合并扩展:)

在您的结果之后,您想要这样的东西吗

        var finalDic = new Dictionary<string, People>();

        foreach (var dic in result) {
            foreach (var key in dic.Keys) {
                People people;
                if (finalDic.TryGetValue(key, out people)) {
                    finalDic[key] = people | dic[key];  
                }
                else {
                    finalDic[key] = dic[key];
                }                   
            }
        }
var finalDic=newdictionary();
foreach(结果中的var dic){
foreach(dic.键中的var键){
人,;
if(最终TryGetValue(输入、输出人员)){
finalDic[key]=人| dic[key];
}
否则{
finalDic[键]=dic[键];
}                   
}
}
//输出

运动员:亚当、巴里、克里斯、哈罗德

艺术家:埃里克、弗兰克、哈罗德

但您可以简单地将第一个dictionary作为初始对象,并使用第二个dictionary执行与上面类似的操作:

        var EUxUSInterest = new Dictionary<string, People>(EUInterest);

        foreach (var element in USInterest) {
            People people;
            if (EUxUSInterest.TryGetValue(element.Key, out people)) {
                EUxUSInterest[element.Key] = people | element.Value;    
            }
            else {
                EUxUSInterest[element.Key] = element.Value;
            }   
        }
var euxusinsiterest=新字典(EUInterest);
foreach(USInterest中的var元素){
人,;
if(euxusinsiterest.TryGetValue(element.Key,out-people)){
euxusinsiterest[element.Key]=人| element.Value;
}
否则{
EUxUSInterest[element.Key]=element.Value;
}   
}
//输出相同

运动员:亚当、巴里、克里斯、哈罗德


艺术家:Eric、Frank、Harold

嗯,这可能不是最有效的方法(我将研究HashSet的想法)。关于这个查询,我的问题的答案是我应该在foreach循环中使用它。我试图通过访问某些属性来使用查询

此答案使用上述问题中提到的MergeLeft字典扩展名

[Flags]
enum People
{
    None = 0x0, Adam = 0x1, Barry = 0x2, Chris = 0x4, David = 0x8, 
    Eric = 0x10, Frank = 0x20, George = 0x40, Harold = 0x80, Ian = 0x100, 
    Joe = 0x200, Larry = 0x400, Mark = 0x800, Nathan = 0x1000, Oscar = 0x2000, 
    Paul = 0x4000, Roger = 0x8000, Sam = 0x10000, Tom = 0x20000, 
    Victor = 0x40000, Walter = 0x80000,Yogi = 0x100000, Zane = 0x200000
};

Dictionary<string, People> EUInterest = new Dictionary<string, People>() { 
{ "athletes", People.Adam | People.Barry }, 
{ "artists", People.Frank | People.Harold } };
Dictionary<string, People> USInterest = new Dictionary<string, People>() { 
{ "athletes", People.Chris | People.Harold }, 
{ "artists", People.Eric } };

var query = from interest in EUInterest.Keys
                where USInterest.ContainsKey(interest)
                let v1 = EUInterest[interest]
                let v2 = USInterest[interest]
                select new Dictionary<string, People>() { {interest, v1 | v2 } };

Dictionary<string, People> temp = new Dictionary<string, People>();
foreach (var a in query)
{
    temp = temp.MergeLeft(a);
}

foreach (People option in Enum.GetValues(typeof(People)))
{
    if (temp["athletes"].HasFlag(option))
        Console.WriteLine("{0} IS AN ATHLETE.", option);
    // else Console.WriteLine("{0} is not an athlete.", option);
}
[标志]
列举人口
{
None=0x0,Adam=0x1,Barry=0x2,Chris=0x4,David=0x8,
Eric=0x10,Frank=0x20,George=0x40,Harold=0x80,Ian=0x100,
乔=0x200,拉里=0x400,马克=0x800,内森=0x1000,奥斯卡=0x2000,
Paul=0x4000,Roger=0x8000,Sam=0x10000,Tom=0x20000,
维克多=0x40000,沃尔特=0x80000,约吉=0x100000,赞恩=0x200000
};
Dictionary EUInterest=新字典(){
{“运动员”,人们,亚当,人们,巴里},
{“艺术家”,人物。弗兰克|人物。哈罗德};
Dictionary USInterest=新建Dictionary(){
{“运动员”,人们,克里斯|人们,哈罗德},
{“艺术家”,人物。埃里克};
var query=来自EUInterest.Keys中的兴趣
其中USInterest.ContainsKey(利息)
设v1=EUInterest[利息]
设v2=USInterest[兴趣]
选择new Dictionary(){{interest,v1 | v2};
Dictionary temp=新字典();
foreach(查询中的变量a)
{
温度=左侧温度(a);
}
foreach(Enum.GetValues(typeof(People))中的人员选项)
{
if(临时[“运动员”].HasFlag(选项))
WriteLine(“{0}是运动员。”,选项);
//else Console.WriteLine(“{0}不是运动员。”,选项);
}

您可以将其转换为
哈希集
,然后使用并集合并这些值,然后保留相同的键。为什么不使用字典?我不知道如何将其转换为哈希集,但我将对此进行研究。是的,我想我正试图让它变得比需要的更困难。我唯一的问题是,根据它创建的错误迭代的数量,这个迭代是否会很昂贵。是的,我正在使它变得比需要的更困难,因为我下面的解决方案基本上包含了一组中间步骤,以完全实现您的解决方案在一开始所做的事情。我明白了,我刚刚得到了最后一个对象,但我相信在原始结果查询之前,您可以通过执行类似的操作来改进,请参阅更新