1

我对LINQ不熟悉,有人可以帮我吗?提前致谢。

我的模型:

public class OrderOverViewModel
{
    public int Quantity { get; set; }
    public String ACCN { get; set; }
    public DateTime OrderDate { get; set; }
}

这是 SQL:

SELECT COUNT(*) AS HowMany, 
       DATEADD(dd, 0, DATEDIFF(dd, 0, RXS_OrderDate)) AS OrderDate, 
       RXS_ACCN  AS ACCN  
FROM RXS_RxJobs
WHERE DATEDIFF(WK, RXS_OrderDate, GETDATE()) = 1
GROUP BY RXS_ACCN, DATEADD(dd, 0, DATEDIFF(dd, 0, RXS_OrderDate))

SSMS 中的结果:

在此处输入图像描述

我希望选择结果填充模型,如何实现这一点?再次感谢。

4

2 回答 2

3

使用SqlFunctions.DateDiff方法获取当前日期和订单日期之间的周数。Date您也可以通过对象的属性简单地获取日期的日期部分DateTime。查询应如下所示:

from j in context.RXS_RxJobs
    .Where(x => SqlMethods.DateDiffDay(x.RXS_OrderDate, DateTime.Now) >= 7 &&
                SqlMethods.DateDiffDay(x.RXS_OrderDate, DateTime.Now) < 14)
group j by new { j.RXS_ACCN, j.RXS_OrderDate.Date } into g
select new OrderOverViewModel
{
   Quantity = g.Count(),
   ACCN =  g.Key.RXS_ACCN,
   OrderDate = g.Key.Date  
};

更新(对于 Linq to Entity):

from j in context.RXS_RxJobs
    .Where(x => SqlFunctions.DateDiff("wk", x.RXS_OrderDate, DateTime.Now) == 1)
group j by new { 
    j.RXS_ACCN, 
    Date = EntityFunctions.TruncateTime(j.RXS_OrderDate).Value
} into g
select new OrderOverViewModel {
   Quantity = g.Count(),
   ACCN =  g.Key.RXS_ACCN,
   OrderDate = g.Key.Date 
};
于 2013-03-13T11:19:50.033 回答
0
var viewModels = from job in ObjectContext.RXS_RxJobs
where SqlFunctions.DateDiff("Day", RXS_OrderDate,DateTime.Now) == 1
group job by new { RXS_ACCN, OrderDate} into jobGroup
select new OrderOverViewModel
{ 
    Quantity = jobGroup.Count(),
    ACCN =  jobGroup.Key.RXS_ACCN,
    OrderDate = jobGroup.Key.OrderDate
} ;
于 2013-03-13T11:06:20.273 回答