c# 4.0 - Where predicate and Expression<Func<T, bool>> -
i have line of code returns index of particular object in ilist<t>
int index = list.indexof(list.where(x => x.code == searchvalue).firstordefault());
and have similar construction on many places, searches collections on different properties. goal automate this, can have generic method myclass<t>
int index = myclass.find<t>(x=> x.code == searchvalue);
or
int index = myclass.find<t>(x => x.name.toupper().startswith(searchvalue.toupper()));
is possible lambda expressions?
edit:
for asking same, here code working:
public int find(func<t, bool> whereclause) { return _list.indexof(_list.where<t>(whereclause).firstordefault<t>()); }
i'm not sure why think need use expression tree. assuming list
list<t>
, should able use findindex
:
int index = list.findindex(x => x.code == searchvalue);
if that's not need, please give more information types involved are.
Comments
Post a Comment