Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find item in IList with LINQ

Tags:

c#

.net

list

linq

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.

like image 620
Frenchi In LA Avatar asked Mar 07 '13 20:03

Frenchi In LA


1 Answers

Simple:

IList list = MyIListMethod();

var item = list
    .Cast<object>()
    .SingleOrDefault(i => i is MyType);

or:

IList list = MyIListMethod();

var item = list
    .Cast<object>()
    .SingleOrDefault(i => i != null);

hope this help!

like image 134
T-moty Avatar answered Oct 02 '22 23:10

T-moty