本文介绍了如何将数据分组到工作日?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将我的数据分组为星期几,例如

周六,周日,周一,周二,周三,周四,周五



使用linq到Entity Framework。

How can i group my data into days of week like
Saturday,Sunday,Monday,Tuesday,Wednesday,Thursday,Friday

using linq to Entity Framework.

推荐答案

var results = data.GroupBy(i =>SqlFunctions.DatePart("weekday", i.DateFieldName));



[]


private List<datetime> days = new List<datetime>();

private void TestGroupByDayOfWeekNotWeekend(object sender, EventArgs e)
{
    for (int i = 1; i < 31; i++)
    {
        int mod = i%7 + 1;
        days.Add(new DateTime(2014,11,mod));
    }

    var dayOfWeekGroups = days
        .Where(dy =>
         dy.DayOfWeek != DayOfWeek.Saturday && dy.DayOfWeek != DayOfWeek.Sunday)
        .GroupBy(dt => dt.DayOfWeek)
        .Where(grp => grp.Count() > 1);

    foreach (var grp in dayOfWeekGroups)
    {
        Console.WriteLine(grp.First().DayOfWeek.ToString());

        foreach (var dt in grp)
        {
            Console.WriteLine('\t' + dt.ToShortDateString());
        }

        Console.WriteLine();
    }
}


这篇关于如何将数据分组到工作日?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-11 16:05