title: LINQ to Entity Query - duplicates in a group
body: <p>I have the following table that holds a list of shifts and the user assigned to each of those shifts as below:</p>
  
  <pre><code>ShiftID | StartDateTime | EndDateTime | AssignedUserID |
  -------------------------------------------------------------
  </code></pre>
  
  <p>What I am trying to do is pull out the shiftID for all shifts that have been assigned to a user on the same date and time. This should not happen as a person cannot be in two places at once...(this one has been sorted...now)</p>
  
  <p>We have some instances in our database where the same user has been assigned to work on 2 different shifts on the same date and time. </p>
  
  <p>e.g.</p>
  
  <pre><code>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
  </code></pre>
  
  <p>Hope someone can help with this, even as a start to pull out the users who have been assigned to shifts on the same start date ?</p>
  
  <pre><code>public IList&lt;ShiftDate&gt; GetDuplicateShifts()
  {
      return _UoW.ShiftDate.Get()
          .ToList();
  }
  </code></pre>
  
  <p>UPDATE</p>
  
  <p>OK, I'm getting there, I am now able to pull out all groups of users where the user is on 2 dates on the same day, the code below works:</p>
  
  <pre><code>public IList&lt;ShiftDate&gt; GetDuplicateShiftsByOrg(int orgID)
  {
      IList&lt;ShiftDate&gt; AllDates = _UoW.ShiftDates
          .Get(s =&gt; s.Shift.organisationID == orgID)
          .Where(s=&gt;s.assignedUserID != null)
          .ToList();
  
      var DuplicateDates = new List&lt;ShiftDate&gt; { };
  
      var groups = AllDates.GroupBy(s=&gt;s.assignedUserID.Value).Where(g =&gt; g.Skip(1).Any());
  
      foreach (var group in groups)
      {
          var group2 = group.GroupBy(sd =&gt; sd.shiftStartDate.Date).Where(a =&gt; a.Count() &gt; 1).ToList();
  
          if (group2.Count() &gt; 1)
          {
  
  
          ///// REF 1 : this pulls out all shiftdates in group one, I want all shiftdates in group2.
  
              foreach (ShiftDate shiftDate in group)
              {
                      DuplicateDates.Add(shiftDate);
              }
  
  /////////////////////////////////////////////////////////
  
          }
      }
  
      return DuplicateDates.ToList();
  }
  </code></pre>
  
  <p>As you will see at REF one above in the code I want to pull out the shiftdates in group2. However when I try to do this I get the following error:</p>
  
  <blockquote>
    <p>Unable to cast object of type 'Grouping[System.DateTime,MySolution.POCO.ShiftDate]' to type 'MySolution.POCO.ShiftDate'.</p>
  </blockquote>
  
  <p>UPDATE</p>
  
  <p>The error above was appearing because I was trying to return a shiftdate from a group of shift dates, I had to add an extra loop to cycle through the groups first. See solution below</p>