C# ASP.NET实体框架SqlQuery不工作
我有一个SQL查询,当我在SQL Server中运行它时,它可以正常工作。现在我想将此查询与实体框架一起使用,如下所示:C# ASP.NET实体框架SqlQuery不工作,c#,asp.net,entity-framework,C#,Asp.net,Entity Framework,我有一个SQL查询,当我在SQL Server中运行它时,它可以正常工作。现在我想将此查询与实体框架一起使用,如下所示: ViewBag.TimeSlots = dbTimeSlots.Data.SqlQuery("SELECT a.id, concat(a.dateSlot, ' - ', a.timeSlot) as dateTimeSlot, sum(IIF(b.dateSlot is null,0,1)) as counter FROM VIP_Preview_TimeSlots as
ViewBag.TimeSlots = dbTimeSlots.Data.SqlQuery("SELECT a.id, concat(a.dateSlot, ' - ', a.timeSlot) as dateTimeSlot, sum(IIF(b.dateSlot is null,0,1)) as counter FROM VIP_Preview_TimeSlots as a LEFT OUTER JOIN [CP-VIP-Preview] as b ON a.dateSlot = b.dateSlot AND a.timeSlot = b.timeSlot GROUP BY a.timeSlot, a.dateSlot, a.[order], a.id Having sum(IIF(b.dateSlot is null,0,1)) < 30 ORDER BY a.[order]").ToList();
这是我的班级:
public class VIP_Preview_TimeSlots
{
public int id { get; set; }
[DisplayName("Time Slots")]
public string timeSlot { get; set; }
[DisplayName("Date Slots")]
public string dateSlot { get; set; }
public int order { get; set; }
}
public class VIPPreviewTimeSlots : DbContext
{
public DbSet<VIP_Preview_TimeSlots> Data { get; set; }
}
我得到了相同的错误。您的SQL查询不会将
日期时隙
和时隙
作为单个列返回,而是将它们连接到日期时隙+'-'+时隙
作为日期时隙
从中调整SQL查询
concat(a.dateSlot, ' - ', a.timeSlot) as dateTimeSlot
致:
或
删除
timeSlot
和dateSlot
属性,并将它们替换为dateTimeSlot
属性。SQL查询不会将dateSlot
和timeSlot
作为单独的列返回,而是将它们连接到dateSlot+-'+timeSlot
作为dateTimeSlot
从中调整SQL查询
concat(a.dateSlot, ' - ', a.timeSlot) as dateTimeSlot
致:
或
删除
时隙
和日期时隙
属性,并将它们替换为日期时隙
属性。您将id
、日期时隙
和计数器
作为查询返回的列,无时隙
查询返回的列有id
、dateTimeSlot
和counter
,没有timeSlot
实体框架代码正在查找一个时隙属性,该属性在您的select查询中似乎缺失。实体框架代码正在查找一个时隙属性,该属性在您的select查询中似乎缺失。如果是,则这是您的错误,实体框架正在查找时隙
和日期时隙
,不是日期时隙
如果是,那么这就是您的错误,实体框架正在查找时隙
和日期时隙
,而不是日期时隙
concat(a.dateSlot, ' - ', a.timeSlot) as dateTimeSlot
a.dateSlot, a.timeSlot