c# - How to use Linq to find dates in listed but not in another -
i have 2 list objects: 1 describes list of shifts , describes list of sick leave.
what trying compare 2 lists , return result gives list of sick leave, dates in sick-leave not in list of shifts.
i have written code, not return list:
var nonavailabledates =availability .where(a=>! shiftday.any(b=>b.shiftdate.date == a.startperiod.date)).tolist();
can please show me have gone wrong?
thanks
you're looking set difference operation, except() method in linq (will give elements in first collection not in second):
list<datetime> shifts = new list<datetime>{ datetime.parse("1/1/2014"), datetime.parse("1/2/2014")}; list<datetime> sickleaves = new list<datetime>{datetime.parse("1/1/2014"), datetime.parse("1/3/2014")}; var sickleavesthatarenotalsoshifts = sickleaves.except(shifts); // contains 1/3/2014
edit: give shot. we're joining , taking items don't have match.
var sickleavesnotinshifts = sickleave in sickleaves join shift in shifts on sickleave.startperiod equals shift.shiftdate joinedshifts js in joinedshifts.defaultifempty() js == null select sickleave;
note: asymptotically better doing nested searches, since join internally implements hash join, o(m+n) (m , n being counts 2 collections).
Comments
Post a Comment