21

Trying to work out a linq query and was wondering if you guys could help.

I have a list of objects foo and each foo object has a list of bar. bar has an active date and a numeric value. for example

foo
   bar -> 01/02/05, 10000
   bar -> 04/06/10, 30023
foo
   bar -> 30/01/02, 23494

And I want to write a linq query that will return me a distinct list of dates and the summed total for that date

It may be that its friday, but I'm drawing a blank.

Thanks in advance

JustLearning
  • 3,164
  • 3
  • 35
  • 52
mat-mcloughlin
  • 6,492
  • 12
  • 45
  • 62

2 Answers2

45

Looks like you want:

var query = from foo in list
            from bar in foo.Bars
            group bar.Value by bar.Date into dates
            select new { Date = dates.Key, Sum = dates.Sum() };
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
6

Check it as well...

var q = from e in Entries
          select new
          {
               EntryID = e.EntryID,
               EntryName = e.EntryName,
               EntryDescription = e.EntryDescription,
               VoterID = voterID,
               Score = (int?)(from v in e.Votes
                              where v.VoterID == voterID 
                              select v.Score).FirstOrDefault()
NoWar
  • 36,338
  • 80
  • 323
  • 498