[英]C# - Linq to SQL , type conversion error
new to LINQ & would like to know why the error & best way to resolve. LINQ的新手,想知道为什么错误和解决的最佳方法。
I am getting, 'Cannot implicitly convert type 'System.Collections.Generic.IEnumerable' to 'System.Collections.Generic.List'. 我得到,'不能隐式地将类型'System.Collections.Generic.IEnumerable'转换为'System.Collections.Generic.List'。 An explicit conversion exists (are you missing a cast?)' error 存在显式转换(您是否错过了演员?)'错误
List<string> names = new List<string>();
names.Add("audi a2");
names.Add("audi a4");
List<string> result1 = new List<string>();
result1=(from name in names
where name.Contains("a2")
select name);
The result of the from is an IEnumerable, you need to create a list to hold it. from的结果是IEnumerable,你需要创建一个列表来保存它。
result1=(from name in names
where name.Contains("a2")
select name).ToList();
so you can simply: 所以你可以简单地说:
List<string> result1 = (from name in names
where name.Contains("a2")
select name).ToList();
Throw on .ToList() at the end of your Linq query. 在Linq查询的末尾抛出.ToList()。
result1=(from name in names
where name.Contains("a2")
select name).ToList();
或者使用Linq的函数语法:
result1 = names.where(x => x.Contains("a2")).ToList();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.