使用Linq从表中计算星期一,星期二等的数量 [英] Count number of Mondays, Tuesdays etc from table using Linq

查看:87
本文介绍了使用Linq从表中计算星期一,星期二等的数量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试找出如何使用Linq和C#在表中计算星期一,星期二等的数量

I'm trying to figure out how to count the number of Mondays, Tuesdays etc in a table using Linq and C#

这是我的示例数据:

Status      StatusDate

DELIVRD     2015-04-16 11:57:47.000

DELIVRD     2015-04-16 13:02:57.000

我知道我需要使用分组依据"对与1相同的星期一,星期二等进行分组.

I know I need to use Group by to group the same Mondays, Tuesdays etc as 1.

我的尝试:

var mondays = rad.SMSSentItems
    .Where(x => (x.StatusDate.Value.DayOfWeek == DayOfWeek.Monday) 
             && (x.Status == "DELIVRD"))
    .ToList()
    .Count;

推荐答案

您需要按所需的状态(DELIVRD)进行过滤,然后按状态日期的DayOfWeek进行分组

You need to filter by the desired Status (DELIVRD) then group them by DayOfWeek of the status date

var weekDays = rad.SMSSentItems
    .Where(x => x.Status == "DELIVRD")
    .AsEnumerable()
    .GroupBy(x => x.StatusDate.Value.DayOfWeek)
    .Select(g => {
        //Total items sent on this day of the week
        var totalItemCount = g.Count();
        //Total number if this distinct day of the week
        var totalNumberOfDays = g.Select(x => x.StatusDate.Value.Date).Distinct().Count();
        return new {
            DayOfWeek = g.Key,
            TotalItemCount = totalItemCount,
            TotalNumberOfDays = totalNumberOfDays,
            AverageItemPerDay = totalItemCount / totalNumberOfDays
        };
    })
    .ToList();

这篇关于使用Linq从表中计算星期一,星期二等的数量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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