89

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>.

bluish
  • 26,356
  • 27
  • 122
  • 180
Pankaj
  • 4,419
  • 16
  • 50
  • 72
  • 9
    it's best to program against the interface (IList) instead of the implimentation (List). It's an unneeded cast to go to List. You now have to add error handling if on the off chance an implementation of IList, which is not a List, enteres that code path. – Brian Leahy Feb 05 '10 at 14:35
  • 8
    Yeah, but `IList` doesn't have stuff that `List` has, for example `AddRange` – Serj Sagan Nov 20 '17 at 16:45

9 Answers9

154

Try

List<SubProduct> subProducts = new List<SubProduct>(Model.subproduct);

or

List<SubProduct> subProducts = Model.subproducts as List<SubProduct>;
Pbirkoff
  • 4,642
  • 2
  • 20
  • 18
  • 6
    This is the right answer, but is there a reason that you want to deal with a List instead of IList? Most of the times using IList is a better choice. – Keith Rousseau Feb 05 '10 at 14:25
  • 3
    IList subProducts= Model.subproduct; – Brian Leahy Feb 05 '10 at 14:31
  • 5
    @KeithRousseau I ran into the issue when needing to utilize the AddRange() method. – ScubaSteve May 08 '15 at 19:02
  • 16
    The second option is not safe, because it assumes that `Model.subproducts` is already of type `List` under the covers. If it's a different implementation of `IList`, such as `ReadOnlyCollection`, it will silently return null and will likely cause a very confusing `NullReferenceException` later in the program execution. And really, if we know that it's always going to be a `List`, then what's the point of returning an `IList` in the first place? – sstan Nov 27 '17 at 13:42
35

How about this:

List<SubProduct> subProducts = Model.subproduct.ToList();
Mark Seemann
  • 225,310
  • 48
  • 427
  • 736
18

In my case I had to do this, because none of the suggested solutions were available:

List<SubProduct> subProducts = Model.subproduct.Cast<SubProduct>().ToList();
bluish
  • 26,356
  • 27
  • 122
  • 180
5
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).

bluish
  • 26,356
  • 27
  • 122
  • 180
Webleeuw
  • 7,222
  • 33
  • 34
2

If you have an IList containing interfaces, you can cast it like this:

List to IList

List<Foo> Foos = new List<Foo>(); 
IList<IFoo> IFoos = Foos.ToList<IFoo>();

IList to List

IList<IFoo> IFoos = new List<IFoo>();
List<Foo> Foos = new List<Foo>(IFoos.Select(x => (Foo)x));

This assumes Foo has IFoo interfaced.

Jason Landbridge
  • 968
  • 12
  • 17
0
List<ProjectResources> list = new List<ProjectResources>();        
IList<ProjectResources> obj = `Your Data Will Be Here`;
list = obj.ToList<ProjectResources>();

This Would Convert IList Object to List Object.

Peter Bruins
  • 807
  • 9
  • 25
Hudhaifa Yoosuf
  • 869
  • 2
  • 12
  • 28
0

The other answers all recommend to use AddRange with an IList.

A more elegant solution that avoids the casting is to implement an extension to IList to do the job.

In VB.NET:

<Extension()>
Public Sub AddRange(Of T)(ByRef Exttype As IList(Of T), ElementsToAdd As IEnumerable(Of T))
   For Each ele In ElementsToAdd
      Exttype.Add(ele)
   Next
End Sub

And in C#:

public void AddRange<T>(this ref IList<T> Exttype, IEnumerable<T> ElementsToAdd)
{
    foreach (var ele in ElementsToAdd)
    {
        Exttype.Add(ele);
    }
}
Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
-1
public async Task<List<TimeAndAttendanceShift>> FindEntitiesByExpression(Expression<Func<TimeAndAttendanceShift, bool>> predicate)
{
    IList<TimeAndAttendanceShift> result = await _dbContext.Set<TimeAndAttendanceShift>().Where(predicate).ToListAsync<TimeAndAttendanceShift>();
    return result.ToList<TimeAndAttendanceShift>();
}
The_Black_Smurf
  • 5,178
  • 14
  • 52
  • 78
Golden Lion
  • 3,840
  • 2
  • 26
  • 35
-3

This is the best option to cast/convert list of generic object to list of string.

object valueList;
List<string> list = ((IList)valueList).Cast<object>().Select(o => o.ToString()).ToList();
Ghebrehiywet
  • 884
  • 3
  • 12
  • 20