【问题标题】:How to write linq query from sql query using LINQ?如何使用 LINQ 从 sql 查询编写 linq 查询?
【发布时间】:2015-03-20 07:49:42
【问题描述】:

我有一个表,其中包含两列,例如具有 varchar 数据类型的 BookingArrivedEnquiredTime 和日期时间 BookingArrivedEnquiredDateTime。当我在 SQL Server 中执行此查询时,结果与时间排序顺序完美 sql 查询会像

select BookingArrivedEnquiredTime from BookingArriveds where BookingArrivedEnquiredDateTime='2015-02-17 00:00:00.000' 
order by CAST(('01/01/2000 ' + BookingArrivedEnquiredTime) AS DATETIME)

它会像这样输出

11:27 AM
11:47 AM
11:53 AM
12:13 PM
12:50 PM
02:02 PM
02:47 PM
03:04 PM
03:16 PM

当我尝试使用 linq 进行此查询时

public ViewResult Index1(DateTime? Startdate) 
{ 
    Startdate = DateTime.Now.Date; 
    var fm = DateTime.Parse("01/01/2000"); 
    var qr = from item in db.BookingArriveds 
             where item.BookingArrivedEnquiredDateTime == Startdate 
             orderby DateTime.Parse("01/01/2000 " +
             item.BookingArrivedEnquiredTime.ToString()) 
             select item; 
     return View(qr);
}

但它会给出这样的错误

LINQ to Entities 无法识别方法 'System.DateTime Parse(System.String)' 方法,并且无法翻译此方法 进入商店表达式。

哪里出了问题,我需要帮助来了解如何将上面的 sql 查询重写为 linq 查询,同时在 linq 中从 varchar 转换为 datetime?

【问题讨论】:

  • 您正在尝试编写一个将在 SQL 中执行的查询,但您正在使用像 DateTime.Parse() 这样的 C# 方法,SQL 无法识别它们。
  • ok omri ,但是如何在 linq 中编写这个查询?
  • 为什么不稍后解析呢?无论如何,您都可以通过item.BookingArrivedEnquiredTime 订购,因为您使用的前缀日期是相同的,因此仅使用日期而不使用它的订购应该提供相同的结果。
  • 我试过了,但它给出的结果类似于晚上 11 点、上午 10 点、下午 7 点、上午 6 点,它仅按数字排序,而不是基于上午/下午排序
  • 所以......您认为可能不是创建一个全新的 DateTime(例如 DateTime.Parse),而是使用您已经使用的那个有...

标签: c# asp.net-mvc linq entity-framework


【解决方案1】:

正如其他人所回答的那样,这会中断,因为 .ToString 在进入数据库的过程中无法转换为相关的 SQL。

但是,Microsoft 提供了SqlFunctions class,它是可在此类情况下使用的方法集合。

对于这种情况,您在此处查找的是SqlFunctions.StringConvert

public ViewResult Index1(DateTime? Startdate) 
{ 
    Startdate = DateTime.Now.Date; 
    var fm = DateTime.Parse("01/01/2000"); 
    var qr = from item in db.BookingArriveds 
             where item.BookingArrivedEnquiredDateTime == Startdate 
             orderby SqlFunctions.StringConvert("01/01/2000 " +
             item.BookingArrivedEnquiredTime.ToString()) 
             select item; 
     return View(qr);
}

当临时变量的解决方案因任何原因不受欢迎时很好。

【讨论】:

  • 这对字符串字段进行字母排序,这是错误的。他需要将 BookingArrivedEnquiredTime 转换为日期时间
  • 我的工作代码仅有助于解决错误!
【解决方案2】:

你有两个选择:

您可以在客户端进行投射和排序:

db.BookingArriveds
  .Where(item => item.BookingArrivedEnquiredDateTime == Startdate)
  .AsEnumerable()
  .OrderBy(item => DateTime.Parse("01/01/2000 " + item.BookingArrivedEnquiredTime);

或者您可以使用 SqlFunctions.DatePart 在 Sql server 中进行转换:https://msdn.microsoft.com/en-us/library/system.data.objects.sqlclient.sqlfunctions(v=vs.110).aspx

编辑:DatePart 函数不合适,因为它只给你日期部分。要在 SQL Server 中进行转换,您应该定义自己的 sql 函数:https://msdn.microsoft.com/en-us/library/vstudio/dd456847(v=vs.100).aspx

问题是,为什么要将BookingArrivedEnquiredTime 存储在 varchar 列中。我相信它应该是BookingArrivedEnquiredDateTime 的一部分,或者它应该存储为整数或数字列

【讨论】:

  • --->我试过你的查询它工作正常它正在排序上午到下午但它在 12 之后没有排序我将把样本放在这里--> 12:48 AM 06:02 AM 07:06 AM 08:41 AM 09:41 AM 12:38 PM 06:02 PM 07:06 PM 08:41 PM
  • 我在输出中没有看到问题。它按应有的方式排序。中午 12:38 -> 12:38;下午 6:02 -> 18:02 等。
猜你喜欢
  • 1970-01-01
  • 2021-01-15
  • 2013-04-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多