简体   繁体   English

实体框架将字符串值转换为日期时间

[英]Entity Framework convert string value to datetime

I have a table storing a list of dates & time (dd/MM/yyyy) in string format.我有一个表格以字符串格式存储日期和时间(dd/MM/yyyy)列表。

What I am trying to do is order the data by DateTime descending, the code below worked just fine but it just orders it by date (dd), So I have to convert it to Datetime to do the right job.我想做的是按DateTime时间降序排列数据,下面的代码工作得很好,但它只是按日期(dd)排序,所以我必须将它转换为日期时间才能完成正确的工作。

casesBindingSource.DataSource = db.casesSet
                                  .Where(x => x.casestatus == "شفيت")
                                  .OrderByDescending(c => c.dateofrecovery)
                                  .ToList();

try:尝试:

casesBindingSource.DataSource = db.casesSet
                                  .Where(x => x.casestatus == "شفيت")
                                  .OrderByDescending(c => DateTime.ParseExact(c.dateofrecovery, "dd/MM/yyyy", Nothing))
                                  .ToList();

Try this approach: .OrderByDescending(c => DateTime.ParseExact(c.dateofrecovery, "dd/MM/yyyy", CultureInfo.InvariantCulture))试试这个方法: .OrderByDescending(c => DateTime.ParseExact(c.dateofrecovery, "dd/MM/yyyy", CultureInfo.InvariantCulture))

Did you try first calling tolist and then running orderby.您是否尝试先调用 tolist 然后运行 orderby。 I know it is a bit inefficient, however it is worth giving a try.我知道这有点低效,但是值得一试。

casesBindingSource.DataSource = db.casesSet
                                  .Where(x => x.casestatus == "شفيت")
                                  .ToList()
                                  .OrderByDescending(c => DateTime.ParseExact(c.dateofrecovery, "dd/MM/yyyy",CultureInfo.InvariantCulture).Year)
                                  .ThenByDescending(c => DateTime.ParseExact(c.dateofrecovery, "dd/MM/yyyy", CultureInfo.InvariantCulture).Month)
                                  .ThenByDescending(c => DateTime.ParseExact(c.dateofrecovery, "dd/MM/yyyy", CultureInfo.InvariantCulture).Day);

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

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