Alex Rivera | Logout

Find item in IList with LINQ

Asked 2013-03-07T20:42:42.540
17

I have an IList:

IList list = CallMyMethodToGetIList();

that I don't know the type I can get it

Type entityType = list[0].GetType();`

I would like to search this list with LINQ something like:

var itemFind = list.SingleOrDefault(MyCondition....);

Thank you for any help.

Edit
Report

1 Answer

8
IList list = ...

// if all items are of given type
IEnumerable<YourType> seq = list.Cast<YourType>().Where(condition);

// if only some of them    
IEnumerable<YourType> seq = list.OfType<YourType>().Where(condition);
answered 2013-03-07T20:51:37.713

Your Answer