new to LINQ & would like to know why the error & best way to resolve.
I am getting, 'Cannot implicitly convert type 'System.Collections.Generic.IEnumerable' to '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 Lis开发者_运维知识库t<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.
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.
result1=(from name in names
where name.Contains("a2")
select name).ToList();
Or using Linq's function syntax:
result1 = names.where(x => x.Contains("a2")).ToList();
精彩评论