title: Complex Linq Grouping
body: <p>I'm new to Stack Overflow, but tried to put as much information</p>
  
  <p>I have following class structure</p>
  
  <pre><code>public class ItemEntity
  {
      public int ItemId { get; set; }
      public int GroupId { get; set; }
      public string GroupName { get; set; }
      public DateTime ItemDate { get; set; }
      public string Field1 { get; set; }
      public string Filed2 { get; set; }
      public string Field3 { get; set; }
      public string Field4 { get; set; }
      public int Duration { get; set; }        
  }
  
  public class MasterEntity
  {
      public ItemEntity Item { get; set; }
      public List&lt;int&gt; ItemList { get; set; }
      public List&lt;int&gt; GroupList { get; set; }
  }
  </code></pre>
  
  <p>I am trying to group list of <code>ItemEntity</code> into <code>MasterEntity</code>. Grouping fileds are Field1,Field2 and Field3.</p>
  
  <p>I have done the grouping so far like below</p>
  
  <pre><code>var items = new List&lt;ItemEntity&gt;
              {
                  new ItemEntity
                  {
                      ItemId = 100,
                      GroupId = 1,
                      GroupName= "Group 1",
                      ItemDate = new DateTime(2018,10,17),
                      Duration = 7,
                      Field1 = "Item Name 1",
                      Filed2 = "aaa",
                      Field3= "bbb",
                      Field4= "abc"
                  },
                  new ItemEntity
                  {
                      ItemId = 150,
                      GroupId = 2,
                      GroupName= "Group 2",
                      ItemDate = new DateTime(2018,10,17),
                      Duration = 5,
                      Field1 = "Item Name 1",
                      Filed2 = "aaa",
                      Field3= "bbb",
                      Field4= "efg"
                  },
                  new ItemEntity
                  {
                      ItemId = 250,
                      GroupId = 3,
                      GroupName= "Group 3",
                      ItemDate = new DateTime(2018,10,15),
                      Duration = 7,
                      Field1 = "Item Name 1",
                      Filed2 = "aaa",
                      Field3= "bbb",
                      Field4= "xyz"
                  }
              };
  
  
              var group = items.GroupBy(g =&gt; new
              {
                  g.Field1,
                  g.Filed2,
                  g.Field3
              }).Select(s =&gt; new MasterEntity
              {
                  Item = new ItemEntity
                  {
                      Field1 = s.Key.Field1,
                      Filed2 = s.Key.Filed2,
                      Field3 = s.Key.Field3
                  },
                  ItemList = s.Select(g =&gt; g.ItemId).ToList(),
                  GroupList = s.Select(g =&gt; g.GroupId).ToList()
              }).ToList();
  </code></pre>
  
  <p>With in this group, I want to further split this by actual ItemDate and Duration so it looks like below </p>
  
  <p><a href="https://i.stack.imgur.com/rbnsx.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/rbnsx.png" alt="Expected Output"></a></p>
  
  <p>Basically, I want to split this group in to three in this case.</p>
  
  <p>As only Group3 is having Date 15th to 17, it will be one group. 
  From 17th to 22nd Group1, Group2 and Group3 are same. so that will become another group.
  And last only Group1 have 22nd to 24 so it become another group</p>
  
  <p>Final grouped data to be like</p>
  
  <pre><code>G1
  {
   ItemEntity :{
   ItemDate : 15/10/2018,
   Duration : 2,
   Field1 : "Item Name 1",
   Filed2 : "aaa",
   Field3 : "bbb",
      },
  ItemList: {250},
  GroupList:{3}
  }
  
  ,
  G2
  {
   ItemEntity :{
   ItemDate : 17/10/2018,
   Duration : 5,
   Field1 : "Item Name 1",
   Filed2 : "aaa",
   Field3 : "bbb",
  },
  ItemList: {100,150,250},
  GroupList:{1,2,3}
  }
  ,
  G3
  {
   ItemEntity :{
   ItemDate : 22/10/2018,
   Duration : 2,
   Field1 : "Item Name 1",
   Filed2 : "aaa",
   Field3 : "bbb",
  },
  ItemList: {100},
  GroupList:{1}
  }
  </code></pre>