I have an instance that contains a List that contains a set of records. I have created another instance of this list where I only want to pull out the active records, but this is the error it throws:
Message=Unable to cast object of type ‘WhereListIterator
1[Namespace.Class]' to type 'System.Collections.Generic.List1[Namespace.Class]’.
Here is my code
public async Task CompareList(List<ListClass> listInstance1)
{
List<ListClass> listInstance2 = new List<ListClass>();
listInstance2 = (List<ListClass>)listInstance1.Where(x => x.Active == "A");
}
>Solution :
the problem in your code is that you try to cast IEnumerable<T> to List<T>
you have two options, either use .ToList() as suggested in the other answer, or pass the filtered list as a parameter to construct a new list as shown in the following code:
public async Task CompareList(List<ListClass> listInstance1)
{
List<ListClass> listInstance2 = new(listInstance1.Where(x => x.Active == "A"));
}