Cast IList to List

C#ListCastingIlist

C# Problem Overview


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

C# Solutions


Solution 1 - C#

Try

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

or

List<SubProduct> subProducts = Model.subproducts as List<SubProduct>;

Solution 2 - C#

How about this:

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

Solution 3 - C#

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 - C#

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

Solution 5 - C#

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.

Solution 6 - C#

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);
    }
}

Solution 7 - C#

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.

Solution 8 - C#

    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>();
    }

Solution 9 - C#

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();

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionPankajView Question on Stackoverflow
Solution 1 - C#PbirkoffView Answer on Stackoverflow
Solution 2 - C#Mark SeemannView Answer on Stackoverflow
Solution 3 - C#bluishView Answer on Stackoverflow
Solution 4 - C#WebleeuwView Answer on Stackoverflow
Solution 5 - C#Hudhaifa YoosufView Answer on Stackoverflow
Solution 6 - C#user11331434View Answer on Stackoverflow
Solution 7 - C#Jason LandbridgeView Answer on Stackoverflow
Solution 8 - C#Golden LionView Answer on Stackoverflow
Solution 9 - C#GhebrehiywetView Answer on Stackoverflow