title: Filter By Date in a GroupBy
body: <p>I need some help for a Filter By Date with LINQ query.</p>
  
  <p>So,</p>
  
  <p>This is my Class:</p>
  
  <pre><code>public class MyList
      {
          public string Ent { get; set; }
          public string Con { get; set; }
          public int Count { get; set; }
          public double? Med { get; set; }
          public DateTime Data { get; set; }
      }
  </code></pre>
  
  <p>My Query:</p>
  
  <pre><code>public IEnumerable&lt;MyList&gt; LoadData()
      {
          var ctx = new DbContext();
  
          var query = (from t in ctx.tblTimes.AsQueryable().Where(s =&gt; s.Included == true)
                       join av in ctx.tblPrincipal on t.AID equals av.AID
                       join c in ctx.tblCon on av.ConID equals c.ConID
                       join e in ctx.tblEnt on av.EntID equals e.EntID
                       group t by new
                       {
                           c.Name,
                           e.Entity
                       } into grp
                       select new MyList
                       {
                           Con = grp.Key.Name,
                           Ent = grp.Key.Entity,
                           Count = grp.Count(),
                           Med = grp.Average(s =&gt; s.Time),
                           Data = grp.Select(s =&gt; s.tblPrincipal.Data).FirstOrDefault()
                       });
          if (CmbCon.SelectedItem != null)
          {
              var selected = (tblCon)CmbCon.SelectedItem;
              query = query.Where(s =&gt; s.Name == selected.Name);
          }
          if (CmbEnt.SelectedItem != null)
          {
              var selected = (tblEnt)CmbEnt.SelectedItem;
              query = query.Where(s =&gt; s.Entity == selected.Entity);
          }
          if(DataFrom.Checked &amp;&amp; DataTo.Checked)
          {
              DateTime inicio = DataAgendFrom.Value;
              DateTime fim = DataAgendTo.Value;
              query = query.Where(s =&gt; s.Data &gt;= inicio &amp;&amp; s.Data &lt;= fim);
          }
          return query.ToList();
      }
  </code></pre>
  
  <p>My OnLoad event:</p>
  
  <pre><code>private void Form_OnLoad(object sender, EventArgs e)
      {
          data.DataSource = LoadData();
          data.Columns["Data"].Visible = false;
      }
  </code></pre>
  
  <p>So, if I filter by the 2 ComboBox, the filter works fine and results are showed correctly, but if I filter by Dates, if gives my all, like there's no Filters...</p>
  
  <p>Some images from Inicio and Fim Date...</p>
  
  <p><strong>Inicio</strong></p>
  
  <p><a href="https://i.stack.imgur.com/FlTcy.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/FlTcy.png" alt="enter image description here"></a></p>
  
  <p><strong>Fim</strong></p>
  
  <p><a href="https://i.stack.imgur.com/RkGiK.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/RkGiK.png" alt="enter image description here"></a></p>
  
  <p>In my MSSQL Database, I have 2 records with Date like image below... The date range above perfectly fits 1 of my Database record.</p>
  
  <p><a href="https://i.stack.imgur.com/IYO33.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/IYO33.png" alt="enter image description here"></a></p>
  
  <p>But, When I apply dates in Image (Inicio and Fim), I get the 2 records (Count = 2 and Average = Average of 2 records).
  Any ideas?</p>
  
  <p><strong>EDIT</strong>: After some testings, I found that with range dates above, the query returns all records from database when only should return 1 record that fits range, but with another range (Inicio 2018-06-19 00:00:00; Fim 2018-06-19 23:59:59), no results are returned at all, which is wrong because the second record in database fits in this range! Can't understand, but I suspect the problem is this piece of code: </p>
  
  <pre><code>Data = grp.Select(s =&gt; s.tblPrincipal.Data).FirstOrDefault()
  </code></pre>
  
  <p>This is returning all records if the first record fits date range, if first records does not fit range, no results are returned!</p>
  
  <p>Thanks</p>