Cannot implicity convert type System.Collections.Generic.IEnumerable<B> to System.Collections.Generic.List<B>

c#, generics, ienumerable

Solution

Your query returns an `IEnumerable`, while your method must return a `List<B>`. You can convert the result of your query to a list via the `ToList()` extension method.

public class A
{
   public List<B> Load(Collection coll)
   {
       List<B> list = (from x in coll select new B {Prop1 = x.title, Prop2 = x.dept}).ToList();
       return list;
   }
}

The type of the list should be inferred automatically by the compiler. Were it not, you would need to call `ToList<B>()`.

Problem

With the code below i get this error and need help with how to let method Load return `List<B>` Cannot implicity convert type System.Collections.Generic.IEnumerable to System.Collections.Generic.List ``` public class A { public List<B> Load(Collection coll) { List<B> list = from x in coll select new B {Prop1 = x.title, Prop2 = x.dept}; return list; } } public class B { public string Prop1 {get;set;} public string Prop2 {get;set;} } ```

Original source