Sam Cogan
Sam Cogan

Reputation: 4334

Counting instances of a date

I'm sure there is an easy answer to this, but I can't seem to phrase the search to get the right results.

In my controller, lets say I have a list of instances of class x, which in turn has a member variable of class y, which contains a date variable. What I am trying to do is count how many instances of each date there are, to build a graph. So all I want out of this is an array with 1 row for each different date, and a count of the number of times that date occurred.

Any suggestions on the best way to do this would be appreciated.

Upvotes: 0

Views: 361

Answers (4)

Denys Denysenko
Denys Denysenko

Reputation: 7894

The code might be clunky but this should work for you:

using System;
using System.Collections.Generic;
using System.Linq;

namespace App
{
    class Program
    {
        static void Main(string[] args)
        {
            List<X> xs = new List<X>
            {
                new X { Y = new Y {D = DateTime.Now}},
                new X { Y = new Y {D = DateTime.Now}},
                new X { Y = new Y {D = DateTime.Now}},
            };

            IEnumerable<DateTime> ds = xs.Select(x => x.Y.D).Distinct();

            var q = from d in ds
                    select new
                    {
                        D = d,
                        Count = xs.Count(x => x.Y.D.Equals(d))
                    };

            foreach (var i in q)
            {
                Console.WriteLine(i);
            }
        }

        class X
        {
            public Y Y { get; set; }
        }

        class Y
        {
            public DateTime D { get; set; }
        }
    }
}

Upvotes: 0

Cristian Chereches
Cristian Chereches

Reputation: 505

Using extensions methods should help. This is the best I can do without knowing the types you have:

ListOfx.Select(x => x.ClassY.Date)

The previous line will give you all dates. Then you can use GroupBy to group them by their value. This should give you a list of lists.

ListOfx.Select(x => x.ClassY.Date).GroupBy(x => x.Date)

I think this should work. I cannot try the code at the moment.

Upvotes: 0

Jon Skeet
Jon Skeet

Reputation: 1500825

It sounds like you want something like this.

var countByDate = items.GroupBy(x => x.Invoice.ReceivedDate) // Or whatever
                       .Select(g => new { Date = g.Key, Count = g.Count() })
                       .OrderBy(pair => pair.Date);
                       .ToArray();

LINQ rocks :)

Upvotes: 4

Tim Schmelter
Tim Schmelter

Reputation: 460158

You can use Linq's Enumerable.GroupBy:

var dayGroups = listX.GroupBy(x => x.Y.DateTimeVar.Date)
                     .Select(g => new { Day = g.Key, Count = g.Count() })
                     .ToArray();

Now you have all you need, the date and the occurence:

foreach(var dayGroup in dayGroups)
{
    Console.WriteLine("Day: {0} Count: {1}", dayGroup.Day.ToString(), dayGroup.Count);
}

Assuming DateTimeVar is the property and you want to group by the day.

Upvotes: 3

Related Questions