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;}
}
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>().
You need to convert the enumeration to a list, there's an extension method to do that for you e.g. try this:
var result = from x in coll select new B {Prop1 = x.title, Prop2 = x.dept};
return result.ToList();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With