frenchie
frenchie

Reputation: 52017

linq goup by day

I have the following grouping statement:

var test = from a in MyDC.Table
  where .....
  group a by a.Date into daygroups
  select new MyModel()
  {
    TheCount = (from c in daygroups
               where c.AppointDate < "the date of the daygroups for this day").Sum( d =>d)
           }

Basically, the query looks in a table for appointments within a certain month and does counts by day for each day of the month. Daygroups groups the results by days so I can do the daily counts. How do I specify the date within the daygroups?

Thanks.

Upvotes: 1

Views: 184

Answers (2)

Bala R
Bala R

Reputation: 108987

Try

 where c.AppointDate <  daygroups.Key

Upvotes: 2

Mikael &#214;stberg
Mikael &#214;stberg

Reputation: 17166

Your date is in daygroups.Key

When grouping with LINQ, the value you are grouping on ends up in the Key property of the IGrouping object, daygroups in your case.

Something like this, perhaps:

var test = from a in MyDC.Table
where .....
group a by a.Date into daygroups
select new MyModel {
   TheCount = daygroups.Where(d => d.AppointDate < daygroups.Key).Count()
}

Upvotes: 0

Related Questions