Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/287.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_Generics_Collections_Covariance - Fatal编程技术网

C# 查找具有不同类型值的两个词典的并集 问题

C# 查找具有不同类型值的两个词典的并集 问题,c#,linq,generics,collections,covariance,C#,Linq,Generics,Collections,Covariance,给定一个基类和两个派生类: class Base{} class Derived0 : Base{} class Derived1 : Base{} 我有两本字典,类型如下: IDictionary<String, Derived0> d0 = ...; IDictionary<String, Derived1> d1 = ...; 但这会产生错误(使用dmcs编译): 它仍然不起作用: Test.cs(15,42): error CS1928: Type `Syst

给定一个基类和两个派生类:

class Base{}
class Derived0 : Base{}
class Derived1 : Base{}
我有两本字典,类型如下:

IDictionary<String, Derived0> d0 = ...;
IDictionary<String, Derived1> d1 = ...;
但这会产生错误(使用
dmcs
编译):

它仍然不起作用:

Test.cs(15,42): error CS1928: Type `System.Collections.Generic.IDictionary<string,Derived0>' does not contain a member `Concat' and the best extension method overload `System.Linq.Enumerable.Concat<Base>(this System.Collections.Generic.IEnumerable<Base>, System.Collections.Generic.IEnumerable<Base>)' has some invalid arguments
/usr/lib/mono/gac/System.Core/4.0.0.0__b77a5c561934e089/System.Core.dll (Location of the symbol related to previous error)
Test.cs(15,42): error CS1929: Extension method instance type `System.Collections.Generic.IDictionary<string,Derived0>' cannot be converted to `System.Collections.Generic.IEnumerable<Base>'
Test.cs(15,42):错误CS1928:Type`System.Collections.Generic.IDictionary'不包含成员`Concat',并且最佳扩展方法重载`System.Linq.Enumerable.Concat(this System.Collections.Generic.IEnumerable,System.Collections.Generic.IEnumerable)`具有一些无效参数
/usr/lib/mono/gac/System.Core/4.0.0.0__b77a5c561934e089/System.Core.dll(与先前错误相关的符号位置)
Test.cs(15,42):错误CS1929:扩展方法实例类型'System.Collections.Generic.IDictionary'无法转换为'System.Collections.Generic.IEnumerable'
原则上,差异在这里并不重要,因为我正在创建一个新的dictionary对象,但我不知道如何在类型系统中表示它。

IDictionary
KeyValuePair
不是变量。。。因此:

var sequence0 = d0.Select(kvp => new KeyValuePair<String, Base>(kvp.Key, kvp.Value));
var sequence1 = d1.Select(kvp => new KeyValuePair<String, Base>(kvp.Key, kvp.Value));
var dictionaryOfBase = sequence0.Concat(sequence1).ToDictionary(kvp => kvp.Key, kvp => kvp.Value);
var sequence0=d0.Select(kvp=>newkeyvaluepair(kvp.Key,kvp.Value));
var sequence1=d1.Select(kvp=>newkeyvaluepair(kvp.Key,kvp.Value));
var dictionaryOfBase=sequence0.Concat(sequence1.ToDictionary)(kvp=>kvp.Key,kvp=>kvp.Value);
IDictionary
KeyValuePair
不是变体。。。因此:

var sequence0 = d0.Select(kvp => new KeyValuePair<String, Base>(kvp.Key, kvp.Value));
var sequence1 = d1.Select(kvp => new KeyValuePair<String, Base>(kvp.Key, kvp.Value));
var dictionaryOfBase = sequence0.Concat(sequence1).ToDictionary(kvp => kvp.Key, kvp => kvp.Value);
var sequence0=d0.Select(kvp=>newkeyvaluepair(kvp.Key,kvp.Value));
var sequence1=d1.Select(kvp=>newkeyvaluepair(kvp.Key,kvp.Value));
var dictionaryOfBase=sequence0.Concat(sequence1.ToDictionary)(kvp=>kvp.Key,kvp=>kvp.Value);

您可以使用以下扩展方法:

    class Base { }
    class Derived0 : Base { }
    class Derived1 : Base { }

    class Program {
        static void Main(string[] args) {
            var d0 = new Dictionary<string, Derived0>();
            var d1 = new Dictionary<string, Derived1>();
            var b = d0.Merge<string, Derived0, Derived1, Base>(d1);
        }
    }

    public static class DictionaryExtensions {
        public static Dictionary<TKey, TBase> Merge<TKey, TValue1, TValue2, TBase>(this IDictionary<TKey, TValue1> thisDictionary, IDictionary<TKey, TValue2> thatDictionary)
            where TValue1 : TBase
            where TValue2 : TBase {
            var resultDictionary = new Dictionary<TKey, TBase>();
            resultDictionary.AddRange(thisDictionary);
            resultDictionary.AddRange(thatDictionary);

            return resultDictionary;
        }

        public static void AddRange<TKey, TBase, TValue>(this IDictionary<TKey, TBase> dictionary, IDictionary<TKey, TValue> dictionaryToAdd) where TValue : TBase {
            foreach (var kvp in dictionaryToAdd) {
                dictionary.Add(kvp.Key, kvp.Value);
            }
        }
    }
类基类{}
类Derived0:基{}
类Derived1:基{}
班级计划{
静态void Main(字符串[]参数){
var d0=新字典();
var d1=新字典();
var b=d0.合并(d1);
}
}
公共静态类字典扩展{
公共静态词典合并(此IDictionary thisDictionary,IDictionary thatDictionary)
其中tValue 1:TBase
其中tValue 2:TBase{
var resultDictionary=新字典();
resultDictionary.AddRange(thisDictionary);
resultDictionary.AddRange(该字典);
返回结果字典;
}
公共静态void AddRange(此IDictionary dictionary,IDictionary dictionaryToAdd),其中TValue:TBase{
foreach(字典中的var kvp添加){
添加(kvp.Key,kvp.Value);
}
}
}

您可以使用以下扩展方法:

    class Base { }
    class Derived0 : Base { }
    class Derived1 : Base { }

    class Program {
        static void Main(string[] args) {
            var d0 = new Dictionary<string, Derived0>();
            var d1 = new Dictionary<string, Derived1>();
            var b = d0.Merge<string, Derived0, Derived1, Base>(d1);
        }
    }

    public static class DictionaryExtensions {
        public static Dictionary<TKey, TBase> Merge<TKey, TValue1, TValue2, TBase>(this IDictionary<TKey, TValue1> thisDictionary, IDictionary<TKey, TValue2> thatDictionary)
            where TValue1 : TBase
            where TValue2 : TBase {
            var resultDictionary = new Dictionary<TKey, TBase>();
            resultDictionary.AddRange(thisDictionary);
            resultDictionary.AddRange(thatDictionary);

            return resultDictionary;
        }

        public static void AddRange<TKey, TBase, TValue>(this IDictionary<TKey, TBase> dictionary, IDictionary<TKey, TValue> dictionaryToAdd) where TValue : TBase {
            foreach (var kvp in dictionaryToAdd) {
                dictionary.Add(kvp.Key, kvp.Value);
            }
        }
    }
类基类{}
类Derived0:基{}
类Derived1:基{}
班级计划{
静态void Main(字符串[]参数){
var d0=新字典();
var d1=新字典();
var b=d0.合并(d1);
}
}
公共静态类字典扩展{
公共静态词典合并(此IDictionary thisDictionary,IDictionary thatDictionary)
其中tValue 1:TBase
其中tValue 2:TBase{
var resultDictionary=新字典();
resultDictionary.AddRange(thisDictionary);
resultDictionary.AddRange(该字典);
返回结果字典;
}
公共静态void AddRange(此IDictionary dictionary,IDictionary dictionaryToAdd),其中TValue:TBase{
foreach(字典中的var kvp添加){
添加(kvp.Key,kvp.Value);
}
}
}

今天,您知道更简洁的语法来实现这一点吗?@Spotted:由于
KeyValuePair
的public surface中没有任何更改,因此您不能在这里执行任何其他操作。也许,将此代码转换为扩展方法。今天,您是否知道一种更简洁的语法来实现这一点?@Spotted:由于在
KeyValuePair
的公共界面中没有任何更改,因此您无法在此处执行任何其他操作。也许,将此代码转换为扩展方法。
var sequence0 = d0.Select(kvp => new KeyValuePair<String, Base>(kvp.Key, kvp.Value));
var sequence1 = d1.Select(kvp => new KeyValuePair<String, Base>(kvp.Key, kvp.Value));
var dictionaryOfBase = sequence0.Concat(sequence1).ToDictionary(kvp => kvp.Key, kvp => kvp.Value);
    class Base { }
    class Derived0 : Base { }
    class Derived1 : Base { }

    class Program {
        static void Main(string[] args) {
            var d0 = new Dictionary<string, Derived0>();
            var d1 = new Dictionary<string, Derived1>();
            var b = d0.Merge<string, Derived0, Derived1, Base>(d1);
        }
    }

    public static class DictionaryExtensions {
        public static Dictionary<TKey, TBase> Merge<TKey, TValue1, TValue2, TBase>(this IDictionary<TKey, TValue1> thisDictionary, IDictionary<TKey, TValue2> thatDictionary)
            where TValue1 : TBase
            where TValue2 : TBase {
            var resultDictionary = new Dictionary<TKey, TBase>();
            resultDictionary.AddRange(thisDictionary);
            resultDictionary.AddRange(thatDictionary);

            return resultDictionary;
        }

        public static void AddRange<TKey, TBase, TValue>(this IDictionary<TKey, TBase> dictionary, IDictionary<TKey, TValue> dictionaryToAdd) where TValue : TBase {
            foreach (var kvp in dictionaryToAdd) {
                dictionary.Add(kvp.Key, kvp.Value);
            }
        }
    }