18

我正在生成报告以向客户展示在 C# 中使用 LINQ。我想表明没有。每种类型的客户。

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

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;

通过上面的查询,我得到了当天注册的最少客户提前谢谢

4

2 回答 2

38
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);
于 2013-10-31T09:59:00.600 回答
1
var query = from c in _customerRepo.Table
            group c by new { c.TypeOfCustomer, c.Date } into g
            orderby g.Key.Date descending,
                    g.Key.TypeOfCustomer == "GUEST" ? 1 :
                    g.Key.TypeOfCustomer == "REGISTERED" ? 2 : 3
            select new {
               g.Key.Date,
               g.Key.TypeOfCustomer,
               Count = g.Count()
            };

如果每日结果不需要按客人 > 注册 > 经理的顺序排列,您可以删除第二次订购:

var query = from c in _customerRepo.Table
            group c by new { c.TypeOfCustomer, c.Date } into g
            orderby g.Key.Date descending
            select new {
               g.Key.Date,
               g.Key.TypeOfCustomer,
               Count = g.Count()
            };
于 2013-10-31T09:50:25.403 回答