简体   繁体   English

实体框架按时间过滤数据

[英]Entity framework filtering data by time

I am so sorry from the question, but I can not take a period from a DateTime . 对于这个问题,我感到很抱歉,但是我不能在DateTime花点时间。 for exemple: If I have date "10.10.2016 7:00", 10.10.2016 10:00" , I need to take only the rows with the time between "6:00" and "8:00". Next is my code by return an error : "can not use TimeOfDay ", help me please 例如:如果我的日期为"10.10.2016 7:00", 10.10.2016 10:00" ,则只需要输入时间在” 6:00“和” 8:00“之间的行。代码返回错误: "can not use TimeOfDay ",请帮助我

ds.TrafficJamMorning = (from row in orderQuery
                        where row.AcceptedTime.TimeOfDay >= new TimeSpan(6, 30, 0) &&
                        row.AcceptedTime.TimeOfDay <= new TimeSpan(9, 30, 0)
                        group row by row.AcceptedTime.Date
                        into grp
                        select new TrafficJamPeriodInfo
                        {
                             CurrentDateTime = grp.Key,
                             ReceptionCount = grp.Count(r => r.OrderOriginId == (int)OrderOrigin.Reception),
                             InternetCount = grp.Count(r => r.OrderOriginId == (int)OrderOrigin.Internet),
                             ExchangeSystemCount = grp.Count(r => r.OrderOriginId == (int)OrderOrigin.ExchangeSystem)
                        }).ToList();

TimeOfDay Is not supported by the linq provider and it does not know how to parse it into sql. linq提供程序不支持TimeOfDay ,并且它不知道如何将其解析为sql。 Use instead DbFunctions.CreateTime : 改用DbFunctions.CreateTime

Also instantiate the timespans before the linq query so you do not instantiate a new object every time 还要在linq查询之前实例化时间跨度,因此您不必每次都实例化一个新对象

var startTime = new TimeSpan(6, 30, 0);
var endTime = new TimeSpan(9, 30, 0);

var result = (from row in orderQuery
              let time = DbFunctions.CreateTime(row.AcceptedTime.Hour, row.AcceptedTime.Minute, row.AcceptedTime.Second)
              where time  >= startTime &&
                    time  <= endTime
              group row by DbFunctions.TruncateTime(row.AcceptedTime) into grp
              select new TrafficJamPeriodInfo
              {
                   CurrentDateTime = grp.Key,
                   ReceptionCount = grp.Count(r => r.OrderOriginId == (int)OrderOrigin.Reception),
                   InternetCount = grp.Count(r => r.OrderOriginId == (int)OrderOrigin.Internet),
                   ExchangeSystemCount = grp.Count(r => r.OrderOriginId == (int)OrderOrigin.ExchangeSystem)
              }).ToList();

Looking again at the question - If all you want to check is that it is between 2 hours then use the Hour property (This won't be nice to write if you want to check for example Hour and Minues and in that case I'd go for my first suggestion) : 再次查看问题-如果您要检查的时间是2小时之间,请使用Hour属性(如果要检查HourMinues的话,这样写就Minues ,在这种情况下,我会提出我的第一个建议)

var result = (from row in orderQuery
              where row.AcceptedTime.Hour >= 6
                    row.AcceptedTime.Hour < 8
              group row by DbFunctions.TruncateTime(row.AcceptedTime) into grp
              select new TrafficJamPeriodInfo
              {
                   CurrentDateTime = grp.Key,
                   ReceptionCount = grp.Count(r => r.OrderOriginId == (int)OrderOrigin.Reception),
                   InternetCount = grp.Count(r => r.OrderOriginId == (int)OrderOrigin.Internet),
                   ExchangeSystemCount = grp.Count(r => r.OrderOriginId == (int)OrderOrigin.ExchangeSystem)
              }).ToList();

I had a similar problem. 我有一个类似的问题。

You can compare the date parts instead. 您可以改为比较日期部分。

where row.Year > s.Year && r.Month > s.Month && row.Day > s.Day 其中row.Year> s.Year && r.Month> s.Month && row.Day> s.Day

I use the following where clause on my IQueryable: 我在IQueryable上使用以下where子句:

var query = dbContext.GetAllItems().AsQueryable();

//... other filters

if(MusBeBetween6and8){
    query = query.Where(item => item.AcceptedTime.Hour > 6 && item.AcceptedTime.Hour < 8);
}

//... other filters

return query.ToList();

Hope it helps. 希望能帮助到你。 This also works for Oracle + Odac. 这也适用于Oracle + Odac。

ds.TrafficJamMorning = (from row in orderQuery
                        where 
                            DbFunctions.DiffMinutes( DbFunctions.TruncateTime(row.AcceptedTime), row.AcceptedTime) >= 6 * 60 + 30 &&
                            DbFunctions.DiffMinutes( DbFunctions.TruncateTime(row.AcceptedTime), row.AcceptedTime) <= 9 * 60 + 30
                        group row by DbFunctions.TruncateTime(row.AcceptedTime)
                        into grp
                        select new TrafficJamPeriodInfo
                        {
                            CurrentDateTime = grp.Key,
                            ReceptionCount = grp.Count(r => r.OrderOriginId == (int)OrderOrigin.Reception),
                            InternetCount = grp.Count(r => r.OrderOriginId == (int)OrderOrigin.Internet),
                            ExchangeSystemCount = grp.Count(r => r.OrderOriginId == (int)OrderOrigin.ExchangeSystem)
                        }).ToList();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM