Sunday, November 2, 2014

Group by Weekly, Quartly Linq to Entity ?



public object function(List<Students> stu)
{
var res=from p in stu
        where p.CurrentStatus=="Present"
 
//STEP 2: Use the function for Grouping as weekly

        group p by (Weekly(p.DateTime))) into Grp
 
//Step 2: if you want to group it by quarterly use replace above line with 
           
    group p by (Quarterly(p.DateTime.Value.Month)) into Grp
 
      
 
        select new ResClass
        {
        label = Grp.Key.ToString(),
        fee  = Grp.Count(x=>x.fee)
         };
}
 

//STEP 1: Make a Function for Week
 public int Weekly(DateTime day)
        {
            return System.Globalization.CultureInfo.CurrentCulture.Calendar.GetWeekOfYear(day, System.Globalization.CalendarWeekRule.FirstFourDayWeek, DayOfWeek.Sunday);
        }
 
//Note if you want your data to group by quarterly use this function
        public int Quarterly(int month)
        {
            if (month == 1 || month == 2 || month == 3)
            {
                return 1;
            }
            if (month == 4 || month == 5 || month == 6)
            {
                return 2;
            }
            if (month == 7 || month == 8 || month == 9)
            {
                return 3;
            }
            else
            {
                return 4;
            }
 
        }
 

No comments:

Post a Comment

Secure you Asp .NET by Web.config & Global.ascx?

Add to Global.ascx protected void Application_BeginRequest(object sender,EventArgs e)     {         //to remove x frame         Resp...