简体   繁体   English

如何合并两列日期和时间,并使用linq将其与给定的DateTime比较

[英]how to combine two columns Date and Time and compare it with given DateTime using linq

I have following columns in database table - Records: 我在数据库表中有以下几列-记录:

Info - varchar(50)
Date - DateTime
Time - varchar(20) , format like hh:mm

Now I have a function 现在我有一个功能

 public GetRecords(DateTime pDateTime)
    {
    // this function should get all the records from above table whose DateTime is more than pDateTime
    }

How can I do using Linq? 如何使用Linq? I know how to write a linq query but how can I combine these columns for each row of table and compare with pDateTime. 我知道如何编写linq查询,但如何将表的每一行的这些列组合在一起并与pDateTime比较。

public List<NewRecord> GetRecords(DateTime pDateTime)
{
    using (var db = YourDbContext())
    {
        var records = new List<Record>();
        return db.Records.Select(r =>
        {
            var record = new Record();
            record.info = r.info;
            var hm = DateTime.Parse(r.Time);
            var date = new DateTime(r.Date.Year, r.Date.Month, r.Date.Day, hm.Hour, hm.Minute);
            record.Date = date;
            return record;
        }).Where(r=>r.Date >pDateTime).ToList();
    }
}

The best solution is to change your Record table to just contain Info and When ; 最好的解决方案是将Record表更改为仅包含InfoWhen ; where When is the Date and Time of recorded info. 其中, When是记录信息的日期和时间。

public IEnumerable<Record> GetRecords(DateTime after)
{
   using (var db = new TheDbContext())
   {
      return db.Records
         .Where(r => r.When > after);
   }
 }

If you can't change the Record schema. 如果您不能更改Record模式。 Then you could do a partial search in the database based on Date and then remove any entries that fall on the search date and not after the search time. 然后,您可以基于Date在数据库中进行部分搜索,然后删除所有在搜索日期而非搜索时间之后的条目。

public IEnumerable<Record> GetRecords(DateTime after)
{
   var afterDate = after.Date;
   using (var db = new TheDbContext())
   {
      var records = db.Records
         .Where(r => r.Date >= afterDate);
      foreach (var r in records)
      {
         if (r.Date > afterDate) yield return r;
         else 
         {
            var when = r.Date + TimeSpan.Parse(r.Time, "hh:mm");
            if (when > after) yeild return r;
         }
      }
   }
 }
  public List<FDetails> GetReocrds(DateTime date)
            {
                var fs = from f in DBContext.RecordsTable
                where f.Date > date 
                && ConvertToIntfun(f.Time.Split(':')[0]) >= date.Hour 
                && ConvertToIntfun(f.Time.Split(':')[1])>date.Minute
                                select new FDetails(f.Id,f.Info, (DateTime)f.Date, f.Time);
                return fs.ToList();
            }
            internal static int ConvertToIntfun(string value)
            {
                int result = 0;
                if (int.TryParse(value, out result))
                {
                    return result;
                }
                else
                {
                    return result;
                }
            }

or 要么

var fs = from f in DBContext.RecordsTable
                            where (new DateTime(f.Date.Value.Year, f.Date.Value.Month, f.Date.Value.Day, ConvertToIntfun(f.FTime.Split(':')[0]), ConvertToIntfun(f.Time.Split(':')[1]),0))>date
                            select new ForecastDetails(forecast.ForecastId, forecast.ForecastSummary, (DateTime)forecast.ForecastDate, forecast.ForecastTime);
            return fs.ToList();

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

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