I created a class derived from List. However when I tried casting the class to another List object I get a runtime error. My code is similar to the one posted below:
You can use this Extension Method
public static M Cast(this IEnumerable input) where M : ICollection, new()
{
var ret = new M();
foreach(var item in input?? Enumerable.Empty())
{
ret.Add(item);
}
return ret;
}
then you can use it like
var qry = (from c in db.Categories
select new CategoryObj
{
CategoryID = c.CategoryID,
CategoryName = c.CategoryName,
Description = c.Description
}).Cast()