Cast IList to List
I am trying to cast IList
type to List
type but I am getting error every time.
List<SubProduct> subProducts= Model.subproduct;
Model.subproduct
returns IList<SubProduct>
.
Solution 1:
Try
List<SubProduct> subProducts = new List<SubProduct>(Model.subproduct);
or
List<SubProduct> subProducts = Model.subproducts as List<SubProduct>;
Solution 2:
How about this:
List<SubProduct> subProducts = Model.subproduct.ToList();
Solution 3:
In my case I had to do this, because none of the suggested solutions were available:
List<SubProduct> subProducts = Model.subproduct.Cast<SubProduct>().ToList();
Solution 4:
List<SubProduct> subProducts= (List<SubProduct>)Model.subproduct;
The implicit conversion failes because List<>
implements IList
, not viceversa. So you can say IList<T> foo = new List<T>()
, but not List<T> foo = (some IList-returning method or property)
.