将sql语句转换为linq - 使用datepart和分组

时间:2014-06-11 19:16:04

标签: c# sql linq linq-to-entities

下面的SQL返回按周更新的记录数 我已经在linq中重新创建了按周返回计数的部分(下方),但我无法获得 startofweek endofweek 列。帮助

SQL

select 
DATEPART(wk, audit_date) week, 
count(audit_date) updatedRec, 

dateadd (week, DATEPART(wk, audit_date),  dateadd (year, DATEPART(year, audit_date)-1900, 0)) - 4 -  datepart(dw, dateadd (week, DATEPART(wk, audit_date),  dateadd (year, DATEPART(year, audit_date)-1900, 0)) - 4) + 1 startofweek,


dateadd (week, DATEPART(wk, audit_date), dateadd (year, DATEPART(year, audit_date)-1900, 0)) - 4 - datepart(dw, dateadd (week, DATEPART(wk, audit_date), dateadd (year, DATEPART(year, audit_date)-1900, 0)) - 4) + 7 endofweek

from itims.Listings_Audit
where audit_type = 'U' and datepart(year,audit_date) = '2014'
Group by DATEPART(wk, audit_date),  DATEPART(year, audit_date)
order by startofweek, week

LINQ的

    var data = (from p in _db.Listings_Audit
            where p.audit_type == "U"
            where p.audit_date.Value.Year == 2014
            group p by new
            {
                Column1 = (int?) SqlFunctions.DatePart("wk", p.audit_date),
                Column2 = (int?) SqlFunctions.DatePart("year", p.audit_date)
            }
            into g
            select new
            {
                week = g.Key.Column1,
                updatedRecCnt = g.Count(p => p.audit_date != null),


            }).OrderBy(g=>g.week).ToList();

2 个答案:

答案 0 :(得分:1)

此Linq2Entities查询将为您提供 startofweek endofweek 。但是,对于大型列表,您可能会在这些计算中看到性能问题。

这使用EntityFunctions.CreateDateTime来计算基于年和周的日期。

Linq查询:

var data = (
    from p in _db.Listings_Audit
    group p by
        new
        {
            Column1 = (int)SqlFunctions.DatePart("wk", p.audit_date),
            Column2 = (int)SqlFunctions.DatePart("year", p.audit_date)
        }
    into g
    select
        new
        {
            week = g.Key.Column1,
            year = g.Key.Column2,
            startOfWeek =
                EntityFunctions.AddDays(
                    EntityFunctions.CreateDateTime(g.Key.Column2, 1, 1, 0, 0, 0),
                    (7 * (g.Key.Column1 - 1))),
            endOfWeek =
                EntityFunctions.AddDays(
                    EntityFunctions.CreateDateTime(g.Key.Column2, 1, 1, 0, 0, 0),
                    (7 * (g.Key.Column1))),
            updatedRecCnt = g.Count(p => p.CreatedDate != null),
        }).OrderBy(g => g.startOfWeek).ToList();
  • 注意:如果您的行包含null个日期,则需要使用GetValueOrDefault()。但是,根据您将示例限制为"2014"的示例,这对您来说不应该是一个问题。

答案 1 :(得分:1)

朋友给了我需要的答案。结果与我的sql输出相匹配。

var data = (from p in _db.Listings_Audit
                    where p.audit_type == "U"
                    where p.audit_date.Value.Year == 2014
                    group p by new
                    {
                        Column1 = (int?)SqlFunctions.DatePart("wk", p.audit_date),
                        Column2 = (int?)SqlFunctions.DatePart("year", p.audit_date)
                    }
                        into g
                        select new
                        {
                            week = g.Key.Column1,
                            updatedRecCnt = g.Count(p => p.audit_date != null),
                            year = g.Key.Column2

                        }).OrderBy(g => g.week).ToList().Select(y => new
            {
                week = y.week,
                y.updatedRecCnt,
                startofweek = new DateTime(y.year.Value, 1, 1).AddDays(-(int)new DateTime(y.year.Value, 1, 1).DayOfWeek).AddDays(y.week.Value * 7 - 7),
                endofweek = new DateTime(y.year.Value, 1, 1).AddDays(y.week.Value * 7 - 1).AddDays(-(int)new DateTime(y.year.Value, 1, 1).DayOfWeek)
            });