簡體   English   中英

在 c# LINQ 中比較日期時間“MM/dd/yyyy hh:mm:ss”和“yyyy-MM-dd hh:mm:ss”

[英]Compare dateTime "MM/dd/yyyy hh:mm:ss" with "yyyy-MM-dd hh:mm:ss" in c# LINQ

如何檢查日期時間大於以下格式的日期時間。

在“CreatedDate”列中具有以下格式值。

Created Date
    2020-03-01 10:59:50.250
    2020-03-01 10:40:39.610
    2020-03-01 10:39:18.087
    2020-02-29 07:39:18.087

 public IQueryable<UserProfile> GetTopUserProfiles(System.DateTime startDateTime)
 {
   return GetDbContext(toUpdate).Get<UserProfile>( w =>w.CreatedDate >= startDateTime);
 }


 var dtstart = DateTime.Now.ToString("yyyy-MM-dd");
     statDateTime = Convert.ToDateTime(dtstart).AddHours(8); //add 8 hours to current date.
    //start DateTime value: 3/1/2020 8:00:00 AM

 var count = _userProfileRepository.Value.GetTopUserProfiles(startDate).Count();

count is returning 0. 
Expected count is 3

基本上,您無需轉換為yyyy-MM-dd

您應該考慮.NET后端和SQL數據庫之間的服務器時間值是否相同。

試試下面的代碼

return GetDbContext(toUpdate).Get<UserProfile>( w => DateTime.Compare(w.CreatedDate,startDateTime) > 0);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM