entity framework - Linq To Entities Compare Value Against List<int> -
i using entity framework 5.0, , have problem linq query. have following method accepts integer value passed query. works fine.
public ilist<tblcoursebooking> getstandardreport(int attendanceid) { return _uow.tblcoursebookingrepo.all .where(cb => cb.attended.equals(attendanceid) .tolist(); }
however, need change method accepts list of integers, , pulls out records attended equal of list of integers. this
public ilist<tblcoursebooking> getstandardreport(list<int> attendanceids) { return _uow.tblcoursebookingrepo.all .where(cb => cb.attended.equals == attendanceids .tolist(); }
i try , use contains or any linq keywords, however, attended single value, not collection, properties available me after dot are
compareto, equals, gethashcode, gettype, gettypecode, tostring
could please help?
thanks time.
use contains
function, match each id against given list:
return _uow.tblcoursebookingrepo.all .where(cb => attendanceids.contains(cb.attended)) .tolist();
in general, keep in mind clause nothing more fancy foreach nested if-statement (a fancy one, though). needs expression evaluates boolean. if had 1 item check, without using linq, you'd come like:
if(attendanceids.contains(myitem.attended))
you can treat linq's clauses in same way, shown above :) if you're stumped, think of how check single time, because linq iterative part you.
update
as mentioned in faisal's answer, wherein
provides similar functionality. haven't used yet, seems more concise approach.
i'm not changing answer though, feel more important point out how can use boolean evaluation in where
clause, should future similar issues might encounter wherein
not relevant.
but nonetheless, use wherein
in particular case :-)
Comments
Post a Comment