当试图按周分组时。只能从LINQ到实体调用此函数。 [英] When try to group by week. This function can only be invoked from LINQ to Entities.

查看:182
本文介绍了当试图按周分组时。只能从LINQ到实体调用此函数。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试执行以下操作时遇到的错误,将数据分组到linq到实体的周数。



The subjected error thrown when try to do the following, "Grouping data into weeks in linq to entity".

public object function(List<Students> stu)
{
var res=from p in stu
        where p.CurrentStatus=="Present"
        group p by (SqlFunction.DatePart("ww",p.AdmissionDate)) into Grp
        
        select new ResClass
        {
        label = Grp.Key.ToString(),
        fee  = Grp.Count(x=>x.fee)
         };
}

推荐答案

看看这里:http://stackoverflow.com/questions/1059737/group-by-weeks-in-linq-to-entities [ ^ ]


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;
            }

        }


这篇关于当试图按周分组时。只能从LINQ到实体调用此函数。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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