如何从linq中的日期时间偏移中获取日期



我有一个表,它的日期时间存储如下

2022-02-01 17:47:50.3483971 +00:00
2022-05-11 18:47:50.3483971 +00:00
2022-05-21 14:40:50.3483971 +00:00

我正试图编写一个linq查询,将比较获得结果的日期,但我只想通过日期,例如从2022/02/01获取记录

所以我试过

var fdate=  query.filteredDate.LocalDateTime.ToShortDateString(); // this gets the date the user passes in.

问题出现在where子句当我尝试这个

filteredResult.Where(x=> fdate >= DateTime.Parse(x.storeddate.LocalDateTime.ToShortDateString()))

然后出错,说不能应用于字符串和日期时间

所以我试了这个

Where(x=> fdate >= x.storeddate.LocalDateTime.ToShortDateString()))

它说不能应用于字符串和字符串

我做错了什么?

我在这里写了一些代码来解决你的问题:

List<DateTime> ListDatesToCompare = new List<DateTime>(); //that list simulate your input list
ListDatesToCompare.Add(new DateTime(2022, 02, 01, 17, 47, 50, 348, DateTimeKind.Utc));
ListDatesToCompare.Add(new DateTime(2022, 05, 11, 18, 47, 50, 348, DateTimeKind.Utc));
ListDatesToCompare.Add(new DateTime(2022, 05, 21, 14, 40, 50, 348, DateTimeKind.Utc));
//this is your filter in string format
string fdate = "2022/02/01"; 
//for this example i've forced the culture info to be sure that the date will be correctly parsed (here some documentation: https://stackoverflow.com/questions/13797727/datetime-and-cultureinfo)
DateTime fDateParsed = DateTime.Parse(fdate, new System.Globalization.CultureInfo("en-EN")); 
//here the result query
List<DateTime> ResultOfQuery = ListDatesToCompare.Where((DateTime CurrentDT) => 
{ 
//you can do this comparison because the variables has the same type.
return CurrentDT >= fDateParsed;
}).ToList();
我希望代码和我的英语是可以理解的,如果你还需要什么,给我发短信。

相关内容

  • 没有找到相关文章

最新更新