0

I have this query that returns variable date_showx as dateTime

 var myList = (from p in db.Full
      group p by p.date_reception into g
      orderby g.Key
      select new
      {
          date_showx = g.Key,
          countx = g.Count()
      }
).take(10).ToList();

In my app I have a frequence button whose values are month, day and hour, and I need the result of my query to match that type, for example on clicking on month I need to show only month and year, on clicking on days show only day and month, by clicking on hour show only hour and day, like Google Analytics one.

gvlasov
  • 18,638
  • 21
  • 74
  • 110
  • I don't understand what exactly you need. If you just want extract hours, days, etc. from date_showx just use `date_showx.Hour`, `date_showx.Day`. You can find all properties here: https://msdn.microsoft.com/en-us/library/system.datetime%28v=vs.110%29.aspx – Piotr Leniartek Mar 01 '15 at 19:12
  • Showing sample input and output would help. – Gert Arnold Mar 01 '15 at 21:47
  • I want to show dates by using order, so i need to keep the same order as dateTime, because if i convert it to string it wont keep dateTime order –  Mar 02 '15 at 09:35

1 Answers1

0

Your question is a bit unclear but I think what you are trying to achieve is to get counts of the items in your collection grouped not by exact dates but rather by parts of the date. So for example your first requirement is "to show only month and year" meaning you want to divide your items into bags labelled after each month + year and get count in each of the bags. This means that you will need to group by more than one thing. Not the whole date but multiple properties within the date. See the example for Month + Year, I'm sure you'll be able to figure out the rest. You can also refer to this answer: C# Linq Group By on multiple columns

var list = new List<DateTime>
        {
            new DateTime(2014, 2, 1),
            new DateTime(2014, 2, 13),
            new DateTime(2014, 2, 22),
            new DateTime(2014, 4, 1),
            new DateTime(2014, 4, 3),
            new DateTime(2014, 7, 1),
            new DateTime(2014, 7, 6),
            new DateTime(2014, 7, 6),
            new DateTime(2015, 2, 1),
            new DateTime(2015, 2, 1),
            new DateTime(2015, 4, 2),
            new DateTime(2015, 4, 1),
            new DateTime(2015, 4, 1),
        };

        var myList = (from p in list
                      group p by new {p.Month, p.Year} into g
                      orderby g.Key.Year, g.Key.Month
                      select new
                      {
                          Year = g.Key.Year,
                          Month = g.Key.Month,
                          Count = g.Count()
                      }
                    ).Take(10).ToList();

        foreach (var item in myList)
        {
            Console.WriteLine("Year: {0}, Month: {1}, Count {2}", item.Year, item.Month, item.Count);
        }
        Console.ReadKey();
Community
  • 1
  • 1
Jakub Rusilko
  • 859
  • 12
  • 17
  • The problem i got here is that Month = g.Key.Month , doesnt work i should use Month = g.Key.Value.Month, which returns string type , so i cant do the order by properly (it doesnt with alphabetic consideration not date) –  Mar 02 '15 at 09:26