简体   繁体   English

如何在 linq 中比较没有时间部分的日期?

[英]How can i compare date without time part in linq?

How can I compare date without Time part in Nullable DateTime?如何在 Nullable DateTime 中比较没有时间部分的日期? In my model I have datetime field as在我的模型中,我将日期时间字段作为

public DateTime? AdjustmentDate { get; set; }

I tried to compare dates as我试图将日期比较为

if (!request.reconciliationDate.Equals(DateTime.MinValue))
    filterResult = filterResult.Where(rd => 
        rd.AdjustmentDate <= request.reconciliationDate.Date).ToList(); 

and reconciliationDate declared asreconciliationDate声明为

public DateTime reconciliationDate { get; set; }

but when I search using reconciliationDate I get the date as {5/20/2020 12:00:00 AM} I enter date as 5/20/2020 .但是当我使用 reconciliationDate 搜索时,我得到的日期为{5/20/2020 12:00:00 AM}我输入的日期为5/20/2020 I am trying to compare with date 5/20/2020 but doesn't find it,我试图与日期5/20/2020进行比较,但没有找到,

How can I take the time part out from nullable DateTime?如何从可为空的 DateTime 中取出时间部分?

I tried DbFunctions.TruncateTime(request.reconciliationDate.Date) but i get DbFunctions doesnt contain definition TruncateTime我试过 DbFunctions.TruncateTime(request.reconciliationDate.Date) 但我得到DbFunctions doesnt contain definition TruncateTime

A Nullable<T> object stores the value of T in the .Value property, and has a handy .HasValue property to let you know if it's not null.一个Nullable<T>对象存储的值T.Value的财产,并有一个方便的.HasValue属性,让你知道,如果它不是空。 So if you're comparing a DateTime?那么,如果您要比较DateTime? with a DateTime , you can do something like:使用DateTime ,您可以执行以下操作:

if (!request.reconciliationDate.Equals(DateTime.MinValue))
    filterResult = filterResult
        .Where(rd =>
            rd.AdjustmentDate.HasValue &&
            rd.AdjustmentDate.Value.Date <= request.reconciliationDate.Date)
        .ToList();

If(dt1.Date == dt2.Date) 这不会工作吗?

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

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