Click here to Skip to main content
15,884,042 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How can i group my data into days of week like
Saturday,Sunday,Monday,Tuesday,Wednesday,Thursday,Friday

using linq to Entity Framework.
Posted
Updated 16-Nov-14 20:47pm
v2
Comments
Manas Bhardwaj 16-Nov-14 4:31am    
And where is thrusday?

you can use SqlFunctions.DatePart:[^]
C#
var results = data.GroupBy(i =>SqlFunctions.DatePart("weekday", i.DateFieldName));

check This similar question and answers for more information [^]
 
Share this answer
 
v2
Comments
Manas Bhardwaj 16-Nov-14 4:32am    
Yes +5!
[no name] 16-Nov-14 7:13am    
Wow! Looks very easy and straightforward +5.
shaprpuff 17-Nov-14 3:40am    
This function can only be invoked from LINQ to Entities. Error accour when trying to use it in linq to entity
DamithSL 17-Nov-14 3:44am    
can you show the code you tried?
shaprpuff 17-Nov-14 5:23am    
public object valuefunc(List<emails> email)
{

var res = email.Where(p=>p.CurrentStatus=="Completed").GroupBy(p=>SqlFunctions.DatePart("weekday",
p.SubmitDate.Value));


return res.ToList();
}
I have not used Entity FrameWork, but here's an example in Linq of grouping by DayOfWeek, and I assume this can be easily used with EF; to make it more fun I've filtered to return only groups of non-weekend days with more than one instance:
C#
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();
    }
}
 
Share this answer
 
v3
Comments
Manas Bhardwaj 16-Nov-14 4:32am    
This will work. +5!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900