简体   繁体   English

在实体框架查询中将字符串时间转换为日期时间或时间跨度

[英]Convert string time to datetime or timespan in entity framework query

I have stored Time string say "12:20 PM" in the database and i want to compare it with current time in linq query (EF). 我已经在数据库中存储了时间字符串,例如"12:20 PM" ,我想将其与linq查询(EF)中的当前时间进行比较。 As i can not convert it using Convert.ToDateTime etc because it can not be converted into any sql expression. 由于我无法使用Convert.ToDateTime等将其转换,因为无法将其转换为任何sql表达式。

I am thinking to write a query like below, but i know this wont help. 我想写一个如下查询,但我知道这不会帮助。 Please advise. 请指教。

List<CompanyScheduler> companySchedulers = 
    context.CompanySchedulers
          .Where(m => m.IsActive && m.Start <= EntityFunctions.AddMinutes(td, m.TimeZoneOffset))
          .Where(m => (m.LastRun.HasValue && EntityFunctions.TruncateTime(m.LastRun) < tdExcludeTime) || (!m.LastRun.HasValue))
          .Where(m => (m.When == (int)When.Daily && (Convert.ToDateTime("01-01-1990 " + m.RecurAt).TimeOfDay < EntityFunctions.AddMinutes(td, m.TimeZoneOffset).Value.TimeOfDay)) ||
                      (m.When == (int)When.Once && !m.LastRun.HasValue) ||
                      (m.When == (int)When.Weekly && m.RecurrEvery.Contains(today)))
          .ToList();

It's not possible below EF 6.x. 在EF 6.x以下是不可能的。 Either use stored procedure or make all conversions before to link query. 可以使用存储过程或进行所有转换,然后再链接查询。

There is not direct way to 'parse' strings to DateTime or TimeSpan in Linq to Entities , but you can do it using SqlFunctions and DbFunctions classes: 没有直接的方法可以将字符串“解析”为Linq到Entities中的 DateTimeTimeSpan ,但是可以使用SqlFunctionsDbFunctions类来实现:

For TimeSpan : 对于TimeSpan

DbFunctions.CreateTime(SqlFunctions.DatePart("hh", timeString),
                       SqlFunctions.DatePart("mi", timeString), 
                       SqlFunctions.DatePart("ss", timeString));

For DateTime : 对于DateTime

DbFunctions.CreateDateTime(SqlFunctions.DatePart("yy", dateString),
                           SqlFunctions.DatePart("mm", dateString),
                           SqlFunctions.DatePart("dd", dateString),
                           SqlFunctions.DatePart("hh", dateString),
                           SqlFunctions.DatePart("mi", dateString),
                           SqlFunctions.DatePart("ss", dateString));

You can load your data in memory (by calling ToArray() or ToList() ) on your collection and use Convert.ToDateTime : 您可以在集合上通过调用ToArray()ToList()将数据加载到内存中,并使用Convert.ToDateTime

var date = DateTime.Today;
var areEqual = dataContext.YourTable.ToList() 
        //now the data is in memory and you can apply whatever projection you want
        .Select(x => new{Date = Convert.ToDateTime(x.DateStringColumn)})
        .All(x => x.Date == date);

However, this can increase the memory usage if the dataset is quite large. 但是,如果数据集很大,则会增加内存使用量。 To avoid this use the Where() method to load only the records you need. 为避免这种情况,请使用Where()方法仅加载所需的记录。

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

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