0

This is my code

var result = (from row in InBoundtable.AsEnumerable()
    where campains.Contains(row.Field<string>("Campaign"))
    group row by new
    {
      Date = row.Field<string>("Date"),
      Slice = row.Field<string>("Slice")
    } into grp
    select new
    {
      SL = grp.Sum((r) => Double.Parse(r["Inb.ServiceLevel"].ToString())) / grp.Count(),
      Date = ((DateTime.Parse(grp.Key.Date.ToString() + " " + grp.Key.Slice.ToString().Split('-')[0])) - epoch).TotalMilliseconds
    }).ToList();

how can I sort the result according to the Date column? from past to current

for example

Habib
  • 219,104
  • 29
  • 407
  • 436
Marco Dinatsoli
  • 10,322
  • 37
  • 139
  • 253

1 Answers1

3

how can I sort the result according to the Date column? from past to current

Use OrderBy

var result = (from row in InBoundtable.AsEnumerable()
              where campains.Contains(row.Field<string>("Campaign"))
              group row by new
              {
                  Date = row.Field<string>("Date"),
                  Slice = row.Field<string>("Slice")
              } into grp
              select new
              {
                  SL = grp.Sum((r) => Double.Parse(r["Inb.ServiceLevel"].ToString())) / grp.Count(),
                  Date = ((DateTime.Parse(grp.Key.Date.ToString() + " " + grp.Key.Slice.ToString().Split('-')[0])) - epoch).TotalMilliseconds
              })
              .OrderBy(r=> r.Date) //HERE
              .ToList();
Habib
  • 219,104
  • 29
  • 407
  • 436
  • that will sort from past to current or from current to past please? I want if I did this: `result[0]` to have `2012` and `result[100]` to have `2014`. ofc I am asking about the idea not the current syntax of `result` – Marco Dinatsoli Aug 18 '14 at 13:29
  • `OrderBy` will sort in Ascending order, so it will be from past to current. If you want order in descending order *(current to last past date)* then use `OrderByDescending` – Habib Aug 18 '14 at 13:30
  • can I order by `date` and **after finishing ordering by date** then order by `Slice` please? – Marco Dinatsoli Aug 18 '14 at 13:34
  • @MarcoDinatsoli, sure you can do that, First use `OrderBy(r=> r.Date)` and for second ordering use `ThenBy(r=> r.Slice)`, But you are not selecting Slice, in your anonymous type. Thus it will not be available, You may select that in your last select statement and then you can apply, `ThenBy` similarly if you want the second order to be in descending order then use `ThenByDescending` – Habib Aug 18 '14 at 13:35
  • You may also see this post http://stackoverflow.com/questions/298725/multiple-order-by-in-linq – Habib Aug 18 '14 at 13:35
  • there is no `Slice` in `r=>r.Slice`. I think that I have to add it to the `select` but I don't know how, please help me in this last thing ? +1 and accepted answer – Marco Dinatsoli Aug 18 '14 at 13:38
  • In the last `select new`, specify `Slice` from previous `grp` – Habib Aug 18 '14 at 13:39
  • @MarcoDinatsoli, yes that should do it. – Habib Aug 18 '14 at 13:41
  • 1
    @MarcoDinatsoli, Glad to be a help – Habib Aug 18 '14 at 13:43