我有一本有价值的字典。我需要阅读每个列表来提取一个字段。但是在foreach循环中得到以下错误
无法将
System.Collections.Generic.List<UserQuery.Employee>类型转换为UserQuery.Employee
如果我使用firstorDefault (在var iValue的注释行中),它就能工作。但是,我将在我正在阅读的字典中检查多条记录(尽管在本例中,我硬编码了1,以测试我的代码)。
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;}
}发布于 2018-01-06 20:41:51
Where接收两次KeyValuePair,当然对两个KeyValuePair返回true,因为您总是问相同的问题。字典中包含密钥== 1吗?是。因此,Where返回的是两个不能转换为单个泛型列表的List<Employee>元素的枚举。
var ivalue = dict1.FirstOrDefault(kvp => dict1.ContainsKey(1));
if(ivalue.Value != null)
foreach (Employee emp in ivalue.Value)
Console.WriteLine(emp.EID);当然,正如注释中所指出的,使用Linq是没有必要的,因为您的代码可以通过
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);发布于 2018-01-06 20:44:32
您当前的代码
var ivalue = dict1
.Where(kvp => dict1.ContainsKey(1)) //TODO: Very strange condition: check it
.Select(x => x.Value);返回IEnumerable<List<Employee>>作为结果(ivalue)。由于您只需要IEnumerable<Employee>,所以必须将结果扁平化:
var ivalue = dict1
.Where(kvp => dict1.ContainsKey(1))
.SelectMany(kvp => kvp.Value); // kvp: we still have key-value pair然而,这不是我们处理字典的方式;您可能正在寻找这样的东西(让我们保存一些Linq:- .Select(emp => emp.EID)):
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)));发布于 2018-01-06 20:45:34
这不是你通常使用字典的方式。你想做的是这样的事情:
if (dict1.ContainsKey(1)) // if the dictionary contains the key
{
var ivalue = dict1[1]; // get the value of that key
}有了这个,你就用了最有效的方法来搜索字典。所以你应该这样做:
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
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);
}
}https://stackoverflow.com/questions/48131651
复制相似问题