Linq 获取客户按日期分组,然后按其类型 [英] Linq Getting Customers group by date and then by their type

查看:30
本文介绍了Linq 获取客户按日期分组,然后按其类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 C# 中的 LINQ 生成报告以显示客户.我想显示没有.每种类型的客户.

I am working on generating report for showing customer using LINQ in C#. I want to show no. of customers of each type.

有 3 种类型的客户注册,客人和经理.我想按注册日期然后按客户类型按客户分组.即如果今天插入了 3 个客人,4 个注册和 2 个经理.明天 4,5 和 6 分别注册.然后报告应显示当天注册的客户数量.每种类型的单独行.

There are 3 types of customer registered, guest and manager. I want to group by customers by registered date and then by type of customer. i.e If today 3 guest, 4 registered and 2 manager are inserted. and tomorrow 4,5 and 6 are registered resp. then report should show Number of customers registerd on the day . separate row for each type.

DATE        TYPEOF CUSTOMER    COUNT
31-10-2013  GUEST              3
31-10-2013  REGISTERED         4
31-10-2013  MANAGER            2
30-10-2013  GUEST              5
30-10-2013  REGISTERED         10
30-10-2013  MANAGER            3

喜欢这个.

var subquery = from eat in _customerRepo.Table
                           group eat by new { yy = eat.CreatedOnUTC.Value.Year, mm = eat.CreatedOnUTC.Value.Month, dd = eat.CreatedOnUTC.Value.Day } into g
                           select new { Id = g.Min(x => x.Id) };




var query = from c in _customerRepo.Table
                        join cin in subquery.Distinct() on c.Id equals cin.Id
                        select c;

通过上述查询,我​​获得了当天注册的最低客户数量提前致谢

By above query I get minimum cutomers registerd on that day Thanks in advance

推荐答案

var query = _customerRepo.Table
     .GroupBy(c => new {Date = c.Date.Date,  Type = c.TypeOfCustomer})
     .Select(g => new 
                   {
                       Date = g.Key.Date,
                       Type = g.Key.Type, 
                       Count = g.Count
                   }
            )
     .OrderByDescending (r = r.Date);

这篇关于Linq 获取客户按日期分组,然后按其类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆