C # - Linq to SQL type conversion error

new for LINQ and would like to know why the error is the best way to solve.

I get: “It is not possible to implicitly convert the type“ System.Collections.Generic.IEnumerable ”to“ System.Collections.Generic.List. "An explicit conversion exists (are you skipping listing?) '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);
+3
source share
3 answers

The result of this is IEnumerable, you need to create a list to store 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();
+6
source

Run .ToList () at the end of your Linq request.

result1=(from name in names
     where name.Contains("a2")
     select name).ToList();
+3
source

Linq:

result1 = names.where(x => x.Contains("a2")).ToList();
+1

Source: https://habr.com/ru/post/1735145/


All Articles