0

I am having a list myTask, in which I want to do a group by Date and calculate the total per date on the Amount.

How can I do that?

public class Task{
public DateTime Date{get;set;}
public string Item {get;set;}
public decimal Amount {get;set;}    
}

 public List<Task> taskList{ get; set; }
learning
  • 11,415
  • 35
  • 87
  • 154

1 Answers1

8

I suspect you want:

var totals = taskList.GroupBy(x => x.Date)
                     .Select(g => new { Date = g.Key,
                                    Total = g.Sum(x => x.Amount) });

Or in one go:

var totals = taskList.GroupBy(x => x.Date, (date, values) => 
                                 new { Date = date,
                                       Total = values.Sum(x => x.Amount) });

Note that this is assuming the Date property really is a date rather than a timestamp. Otherwise you might want x => x.Date.Date as the first argument.

If this isn't what you're after, please say how it differs from what you want.

EDIT: Okay, if you want the items as well - but presumably not the date with each item - you can write:

var totals = taskList.GroupBy(x => x.Date, (date, values) => 
                       new { Date = date,
                             Total = values.Sum(x => x.Amount),
                             Items = values.Select(x => x.Item).ToList(),
                             Amounts = values.Select(x => x.Amount).ToList() });
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • Thanks @Jon, but I want to be able to be able to send all the values, Date, Item, Amount, along with the total to my view. How can I do that? – learning Aug 02 '11 at 06:16
  • 2
    @learning: It's not clear how you would want the result - you're grouping by date and then aggregating... the aggregation result doesn't naturally have the original values. You talk about "sending" all the values - who to? If you could be clearer in the way you want the data represented, we should be able to work out how to perform the transformation. – Jon Skeet Aug 02 '11 at 06:22
  • I want to do a grouping and do the sum as stated above, however, I will also need the values Item, Amount as well, as I will be displaying the values in my view using MVC. I am trying to upload a picture of how to display the values but not able to upload pictures... – learning Aug 02 '11 at 06:27
  • 1
    @learning: But you still haven't said what *shape* you need that data in. Do you need the Item and Amount as a grouping by date as well, for example? If you could post the code which you'd expect to call to *use* this data, it might help us to understand what representation you're looking for. – Jon Skeet Aug 02 '11 at 06:35
  • part of my question is on http://stackoverflow.com/questions/6897205/difficulty-in-displaying-value-returned-by-json, could you please refer to the diagram of how I want to display the data. – learning Aug 02 '11 at 06:45
  • @learning: That still doesn't really help us much in terms of how you're going to represent it. If you're already doing client-side work anyway, why not *just* do the grouping by date in LINQ, and sum the results on the client side? That would probably be simpler. – Jon Skeet Aug 02 '11 at 06:47
  • I prefer to do all the manipulation on the client side and make it clean on the client side. I am not sure, but I guess there is the need of populating another list, maybe something as follows: public class GroupItems { public DateTime Date{get;set;} public Decimal Total { get; set; } public int[] Amount{ get; set; } public string[] Item{ get; set; } } – learning Aug 02 '11 at 06:51
  • I want to be able to read the values as follows: Date, Item[0], Item[1], Total, Amount[0], Amount[1]....Date, Item[0], Item[1], Total, Amount[0], Amount[1].... – learning Aug 02 '11 at 07:01
  • @learning: See my edit. I'm not sure I would personally split out the amounts and items, but it's your call... – Jon Skeet Aug 02 '11 at 07:31
  • Thanks a lot, one last question, how can I map the totals to the taskList at the end? – learning Aug 02 '11 at 08:01
  • @learning: I don't know what you mean - they're *in* the `taskList`. What sort of "mapping" are you talking about? – Jon Skeet Aug 02 '11 at 08:40
  • the grouped values are stored in totals, which is a var, I need to store them in taskList. public List taskList{ get; set; } – learning Aug 02 '11 at 11:13
  • @learning: Well a total *isn't* logically part of a `Task`, so you'll need to create a new type and populate that instead. – Jon Skeet Aug 02 '11 at 11:28
  • the difficulty I am having is to create a new type and fill arrays. Isn`t there a way og mapping the totals on any other type of Task? – learning Aug 02 '11 at 11:33
  • 1
    @learning: No - look at your `Task` type: it has values for date, item and amount... nothing for `Total`. It feels like you're a bit hazy about how all of this fits together... I would take a step back and try to make sure you understand each of the steps involved first. – Jon Skeet Aug 02 '11 at 12:10