Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/317.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

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# 如何选择IEnumerable<;T>;而不是列表<;T>;从ICollection<;T>;?_C#_Linq_Collections_Todictionary - Fatal编程技术网

C# 如何选择IEnumerable<;T>;而不是列表<;T>;从ICollection<;T>;?

C# 如何选择IEnumerable<;T>;而不是列表<;T>;从ICollection<;T>;?,c#,linq,collections,todictionary,C#,Linq,Collections,Todictionary,我有以下几行代码可以正常工作 Dictionary<Guid, List<T>> dictionary = source.ToDictionary( element => element.Id, element => element.Ts.ToList()); Dictionary Dictionary=source.ToDictionary( element=>element.Id, element=>element.Ts.ToList());

我有以下几行代码可以正常工作

Dictionary<Guid, List<T>> dictionary = source.ToDictionary(
  element => element.Id,
  element => element.Ts.ToList());
Dictionary Dictionary=source.ToDictionary(
element=>element.Id,
element=>element.Ts.ToList());
但是,我希望第一行使用IEnumerable,这会产生类型不匹配的错误

Dictionary<Guid, IEnumerable<T>> dictionary = source.ToDictionary( ... );
Dictionary Dictionary=source.ToDictionary(…);
使用ToArray而不是ToList对我没有帮助(得到同样的错误,加上我已经学会了在C#中不使用数组,除非真的、真的需要),而且据我所知,Ts的类型是ICollection

是否可以按照我希望的方式转换整个沙邦,而不必使用硬核和铸造?

使用
AsEnumerable()
,这是一种简便的Linq方法,只需将源铸造返回到
IEnumerable

Dictionary Dictionary=source.ToDictionary(
element=>element.Id,
element=>element.Ts.AsEnumerable());
您也可以尝试铸造:

Dictionary<Guid, IEnumerable<T>> dictionary = source.ToDictionary(
  element => element.Id,
  element => (IEnumerable<T>)(element.Ts));
Dictionary Dictionary=source.ToDictionary(
element=>element.Id,
element=>(IEnumerable)(element.Ts));

如果要避免强制转换,可以使用扩展方法,该方法将返回正确的类型,但也可以将原始实例/实现隐藏在匿名枚举后面


我个人只会使用
element=>element.Ts作为IEnumerable,以避免调用额外的方法来满足有限的泛型推理。

这是因为
字典
不是协变的。因此,即使
List
实现了
IEnumerable
,也不能选择
Dictionary
并将其强制转换为
Dictionary
。为什么?考虑下面的代码:

Dictionary<string, IEnumerable<int>> myDict = new Dictionary<string, List<int>>();
myDict.Add("key", new HashSet<int>());
或提供泛型类型参数以调用:

Dictionary<Guid, IEnumerable<T>> dictionary = source.ToDictionary<Guid, IEnumerable<T>>(
  element => element.Id,
  element => element.Ts.ToList());
Dictionary Dictionary=source.ToDictionary(
element=>element.Id,
element=>element.Ts.ToList());

Hah,我完全忘记了显式类型注释。这是我的赢家。
Dictionary<Guid, IEnumerable<T>> dictionary = source.ToDictionary(
  element => element.Id,
  element => (IEnumerable<T>)element.Ts.ToList());
Dictionary<Guid, IEnumerable<T>> dictionary = source.ToDictionary<Guid, IEnumerable<T>>(
  element => element.Id,
  element => element.Ts.ToList());