首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >不能将Systems.Collections.GenericList<T>转换为T

不能将Systems.Collections.GenericList<T>转换为T
EN

Stack Overflow用户
提问于 2018-01-06 20:34:50
回答 6查看 165关注 0票数 1

我有一本有价值的字典。我需要阅读每个列表来提取一个字段。但是在foreach循环中得到以下错误

无法将System.Collections.Generic.List<UserQuery.Employee>类型转换为UserQuery.Employee

如果我使用firstorDefault (在var iValue的注释行中),它就能工作。但是,我将在我正在阅读的字典中检查多条记录(尽管在本例中,我硬编码了1,以测试我的代码)。

代码语言:javascript
复制
void Main()
{
    var svalue =1;
    List<Employee> emplist = new List<Employee>()
    {
         new Employee{ EID=10, Ename="John"},
         new Employee{ EID=11, Ename="Adam"},
         new Employee{ EID=12, Ename="Ram"}
    };
    List<Employee> emplist1 = new List<Employee>()
    {
         new Employee{ EID=1, Ename="Jo"},
         new Employee{ EID=1, Ename="Ad"},
         new Employee{ EID=1, Ename="Ra"}
    };

    var dict1 = new Dictionary<int, List<Employee>>();
    dict1.Add(1, emplist);
    dict1.Add(2, emplist1);

    //var ivalue = dict1.FirstOrDefault(x => x.Key == svalue).Value.ToList();
    var ivalue = dict1.Where(kvp => dict1.ContainsKey(1)).Select(x => x.Value);

    Console.WriteLine(ivalue);
    foreach (Employee emp in ivalue)
    {
        Console.WriteLine(emp.EID);
    }
}

class Employee
{
    public int EID { get; set;}
    public string Ename {get; set;}
}
EN

回答 6

Stack Overflow用户

回答已采纳

发布于 2018-01-06 20:41:51

Where接收两次KeyValuePair,当然对两个KeyValuePair返回true,因为您总是问相同的问题。字典中包含密钥== 1吗?是。因此,Where返回的是两个不能转换为单个泛型列表的List<Employee>元素的枚举。

代码语言:javascript
复制
var ivalue = dict1.FirstOrDefault(kvp => dict1.ContainsKey(1));
if(ivalue.Value != null)
    foreach (Employee emp in ivalue.Value)
       Console.WriteLine(emp.EID);

当然,正如注释中所指出的,使用Linq是没有必要的,因为您的代码可以通过

代码语言:javascript
复制
List<Employee> empList;
// Try directly to get the List<Employee> that match the Key
// if found you have the empList initialized with the matching list
// otherwise TryGetValue returns false.
if(dict.TryGetValue(1, out empList))
    foreach (Employee emp in empList)
       Console.WriteLine(emp.EID);
票数 0
EN

Stack Overflow用户

发布于 2018-01-06 20:44:32

您当前的代码

代码语言:javascript
复制
 var ivalue = dict1
   .Where(kvp => dict1.ContainsKey(1)) //TODO: Very strange condition: check it
   .Select(x => x.Value);

返回IEnumerable<List<Employee>>作为结果(ivalue)。由于您只需要IEnumerable<Employee>,所以必须将结果扁平化:

代码语言:javascript
复制
 var ivalue = dict1
   .Where(kvp => dict1.ContainsKey(1))
   .SelectMany(kvp => kvp.Value); // kvp: we still have key-value pair

然而,这不是我们处理字典的方式;您可能正在寻找这样的东西(让我们保存一些Linq:- .Select(emp => emp.EID)):

代码语言:javascript
复制
 if (dict1.TryGetValue(1, out var ivalue)) // do we have corresponding value?
   Console.WriteLine(String.Join(Environment.NewLine, ivalue // if yes, print them out
     .Select(emp => emp.EID)));
票数 2
EN

Stack Overflow用户

发布于 2018-01-06 20:45:34

这不是你通常使用字典的方式。你想做的是这样的事情:

代码语言:javascript
复制
if (dict1.ContainsKey(1)) // if the dictionary contains the key
{
    var ivalue = dict1[1]; // get the value of that key
}

有了这个,你就用了最有效的方法来搜索字典。所以你应该这样做:

代码语言:javascript
复制
var dict1 = new Dictionary<int, List<Employee>>();
dict1.Add(1, emplist);
dict1.Add(2, emplist1);

if (dict1.ContainsKey(1))
{
    var ivalue = dict1[1];

    Console.WriteLine(ivalue); // this will print System.Collections.Generic.List<Employee>
    foreach (Employee emp in ivalue)
    {
        Console.WriteLine(emp.EID);
    }
}

另一种选择是使用TryGetValue

代码语言:javascript
复制
var dict1 = new Dictionary<int, List<Employee>>();
dict1.Add(1, emplist);
dict1.Add(2, emplist1);
List<Employee> ivalue = null;
if (dict1.TryGetValue(1, out ivalue))
{
    Console.WriteLine(ivalue); // this will print System.Collections.Generic.List<Employee>
    foreach (Employee emp in ivalue)
    {
        Console.WriteLine(emp.EID);
    }
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48131651

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档