FindIndex()
is an instance method of List<T>
. But Where()
returns an IEnumerable<T>
(or maybe an IQueryable<T>
depending on what classComponents
is in your code).
So you need to convert that Where
result to list before you can call FindIndex()
:
foreach (var distinctClassId in distinctClassIds)
{
var indexes = classComponents.Where(x=> x.ClassId.Equals(distinctClassId)).ToList(). // here -> ToList()
FindIndex(x => x.Id == Constants.Classes.ClassID.ToString() ||
x.Id == Constants.Courses.CoursesID.ToString());
}
But FindIndex()
only gives you the index of the first matching item. Your variable is called indexes
, so you might want to find the indices of multiple matching items. This can be done by using the linq overloads that give you the index, too:
foreach (var distinctClassId in distinctClassIds)
{
var filtered = classComponents.Where(x=> x.ClassId.Equals(distinctClassId)).ToList();
var indexes = filtered.Select((item, index) => new {item, index})
.Where(x => x.item.Id == Constants.Classes.ClassID.ToString() ||
x.item.Id == Constants.Courses.CoursesID.ToString())
.Select(x => x.index);
}
This creates objects of anonymous type containing the item and its index, then checking which items match and finally returning their indices.
Note: if you actually wanted the indices in the source list (classComponents
), you should combine the Where
statements like that:
foreach (var distinctClassId in distinctClassIds)
{
var indexes = classComponents.Select((item, index) => new {item, index})
.Where(x => x.item.ClassId.Equals(distinctClassId) &&
(x.item.Id == Constants.Classes.ClassID.ToString() ||
x.item.Id == Constants.Courses.CoursesID.ToString()))
.Select(x => x.index);
}