1

I have a customers table and I want to return count of rows inserted each month or Customers registered each month. The following code returns only month and record count but I want to record 0 if say for Jan no customers where registered. Thank you.

The following code returns:

Month 2   Count 15
Month 5   Count 11
Month 9   Count 82

I also want to return

Month 1 count 0
Month 3 count 0
so on..

My code:

var query = (from customers in context.customers 
             group customers by customers.RegisterDateTime.Month into g 
             select new
                    { Month = g.Key, Count = g.Count() }
            ).ToList();

foreach (var data in query)
{
    Console.WriteLine( "Month "+ data.Month +"  Count "+ data.Count);
}
0

3 Answers 3

2
var query = (from m in Enumerable.Range(1, 12)
             join c in context.customers on m equals c.RegisteredDateTime.Month into monthGroup
             select new { Month = m, Count = monthGroup.Count() }
            ).ToList();
1
  • That is awesome. Thank you. so if i have a one to many relationship table , customer to many orders in order table. in orders table I want unique item names as count but the date property is in customers table. How can I do that to get same results as you answered. Thanks
    – Rohit
    Sep 5, 2017 at 7:04
1

I assume that you want the range from the lowest month and the highest month.

I see no possiblity (but maybe there is?) to do it inside your query directly. I would add the "0" after the query to fill the gaps between the range with zeros.

So I would add the following code line after your query:

        var lowestKey = result.Min(x => x.Month);
        var highestKey = result.Max(x => x.Month);       

        query = query.Union(
            Enumerable.Range(lowestKey, highestKey - lowestKey)
            .Where(e => !result.Any(r => r.Month == e))
            .Select(s => new { Month = s, Count = 0 })
            ).OrderBy(o => o.Month).ToList();

Since I don't have your complete code, this query maybe need some adjustment.

If you need another range, than you can simple change it.

My complete example look like this:

    static void Main(string[] args)
    {
        // Initialize the list
        var result = new []
        {
            new  { Month = 2, Count = 15 },
            new  { Month = 5, Count = 11 },
            new  { Month = 9, Count = 82 }
        }.ToList();

        // Generate a List with 0 in Range
        var lowestKey = result.Min(x => x.Month);
        var highestKey = result.Max(x => x.Month);          

        result = result.Union(
            Enumerable.Range(lowestKey, highestKey - lowestKey)
            .Where(e => !result.Any(r => r.Month == e))
            .Select(s => new  { Month = s, Count = 0 })
            ).OrderBy(o => o.Month).ToList();


        foreach (var data in result)
        {
            Console.WriteLine("Month " + data.Month + "  Count " + data.Count);
        }

        Console.ReadKey();
    }
4
  • Thanks , It is a SQL DB table customers , Just want to return count of customers registered each month Like January or as month 01 , 3 records/customers March 4 records , May 0 records
    – Rohit
    Sep 5, 2017 at 5:49
  • If you always have the 12 month from January to December, you may change my code Line with the Enumarble.Range to Enumerable.Range(1, 12) Sep 5, 2017 at 5:52
  • hwo can i replace the data source var result = new [] { new { Month = 2, Count = 15 }, new { Month = 5, Count = 11 }, new { Month = 9, Count = 82 } }.ToList(); to SQL List of customers
    – Rohit
    Sep 5, 2017 at 6:09
  • I think, that Wonder Boy now provided you an better solution. It seems really nice :) Sep 5, 2017 at 6:13
1

Hope it helps,

var query = (from customers in context.customers 
         group customers by customers.RegisterDateTime.Month into g 
         select new
                { Month = g.Key, Count = g.Count(x=>x!=null) }
        ).ToList();
1
  • It doesnt change anything, same result
    – Rohit
    Sep 5, 2017 at 6:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.