c# - LINQ to Entity Query - duplicates in a group -
folks,
i need linq entity query.
i have following table holds list of shifts , user assigned each of shifts below:
shiftid | startdatetime | enddatetime | assigneduserid |
what trying pull out shiftid shifts have been assigned user on same date , time. should not happen person cannot in 2 places @ once...(this 1 has been sorted...now)
we have instances in our database same user has been assigned work on 2 different shifts on same date , time.
e.g.
56 | 06/08/2015 13:00:00 | 06/08/2015 17:00:00 | 22
64 | 06/08/2015 13:00:00 | 06/08/2015 17:00:00 | 22
hope can this, start pull out users have been assigned shifts on same start date ?
public ilist<shiftdate> getduplicateshifts() { return _uow.shiftdate.get() .tolist(); }
update
ok, im getting there, able pull out groups of users user on 2 dates on same day, code below works:
public ilist<shiftdate> getduplicateshiftsbyorg(int orgid) { ilist<shiftdate> alldates = _uow.shiftdates .get(s => s.shift.organisationid == orgid) .where(s=>s.assigneduserid != null) .tolist(); var duplicatedates = new list<shiftdate> { }; var groups = alldates.groupby(s=>s.assigneduserid.value).where(g => g.skip(1).any()); foreach (var group in groups) { var group2 = group.groupby(sd => sd.shiftstartdate.date).where(a => a.count() > 1).tolist(); if (group2.count() > 1) { ///// ref 1 : pulls out shiftdates in group one, want shiftdates in group2. foreach (shiftdate shiftdate in group) { duplicatedates.add(shiftdate); } ///////////////////////////////////////////////////////// } } return duplicatedates.tolist(); }
as see @ ref 1 above in code want pull out shiftdates in group2. when try following error:
unable cast object of type 'grouping[system.datetime,mysolution.poco.shiftdate]' type 'mysolution.poco.shiftdate'.
update
the error above appearing because trying return shiftdate group of shift dates, had add loop cycle through groups first. see solution below
try this:
var dt=datetime.now().tostring("mm/dd/yyyy"); var result = t in table1 c.startdate=dt select new {c.assigneduserid};
Comments
Post a Comment