Cast IList to List
Asked Answered
P

9

91

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

Postfree answered 5/2, 2010 at 13:6 Comment(2)
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.Worthwhile
Yeah, but IList doesn't have stuff that List has, for example AddRangeUnintelligible
C
155

Try

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

or

List<SubProduct> subProducts = Model.subproducts as List<SubProduct>;
Climate answered 5/2, 2010 at 13:8 Comment(4)
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.Stewardess
IList<SubProduct> subProducts= Model.subproduct;Worthwhile
@KeithRousseau I ran into the issue when needing to utilize the AddRange() method.Fortuity
The second option is not safe, because it assumes that Model.subproducts is already of type List<SubProduct> under the covers. If it's a different implementation of IList<SubProduct>, such as ReadOnlyCollection<SubProduct>, 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<T>, then what's the point of returning an IList<T> in the first place?Mage
B
35

How about this:

List<SubProduct> subProducts = Model.subproduct.ToList();
Bundelkhand answered 5/2, 2010 at 13:9 Comment(1)
Be aware that ToList() creates a copy with possible performance and memory impact.Farron
T
19

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

List<SubProduct> subProducts = Model.subproduct.Cast<SubProduct>().ToList();
Trabeated answered 17/12, 2013 at 16:52 Comment(0)
W
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).

Westberry answered 5/2, 2010 at 13:8 Comment(0)
E
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.

Epidaurus answered 13/10, 2019 at 15:19 Comment(0)
R
1
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.

Roley answered 9/6, 2016 at 7:26 Comment(0)
D
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);
    }
}
Devora answered 8/4, 2019 at 21:47 Comment(0)
H
-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>();
}
Heyward answered 29/12, 2019 at 21:33 Comment(0)
B
-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();
Briareus answered 27/10, 2017 at 12:1 Comment(1)
valueList must be list of objectNikianikita

© 2022 - 2024 — McMap. All rights reserved.