c# - Linq issues, joining two tables and a collection -
ok, need suck @ linq!
this collection contains x number of time slot dto's given day.
list<timeslotdto> timeslots = new list<timeslotdto>();
the timeslotdto contains datetime slotstart property , bool isreserved. collection works, contains number of timeslots want. fine.
now problem. want use collection in linq query. have 2 tables,"staff" , "timeslots"(well three, if might able add last table query myself)
the "timeslots" table contains:
- staffid(fk)
- timeslotid(pk)
- slotstart
- activityid(fk third table).
the "staff" table contains:
- staffid(pk)
- navigation property timeslots
based on this...i want produce result each staff member has list of timeslots , slots in collection has same date in timeslots table isreserved prop should set true.
so timeslots collection contains possible slots given day. timeslots table contains reservations. want x-check collections find reservations each staff member.
thanks in advance!
if has tutorial on mastering linq, please share it.
you can use contains
statement this:
var query = timeslots.where(sl => timeslots.where(t => t.staffid == staffid) .select(t => t.slotstart) .contains(sl.slotstart));
this (slightly) better scalable if timeslots
contains time slots particular staff.
in query syntax like
var query = sl in timeslots (from t in timeslots t.staffid == staffid select(t => t.slotstart)) .contains(sl.slotstart);
another option timeslots
in memory , join timeslots
. (you can't join local collections linq sql backend.)
from sl in timeslots join t in timeslots.where(x => x.staffid == staffid) .asenumerable() // switch memory on ts.slotstart equals t.slotstart
again assuming timeslots
per staff.
Comments
Post a Comment