运行 SQLQuery 按日期计算行数
Running SqlQuery to count rows by date
我不擅长 Entity Framework,但计划 运行 原始查询。现在我坚持这个。使用以下代码时,出现以下错误。
Error CS1061 'Respons' does not contain a definition for 'TotalCount' and
no extension method 'Total' accepting a first argument of type
'Respons' could be found (are you missing a using directive or an
assembly reference?)
当我 运行 在 SSMS 中进行相同的查询时,我得到了以下结果。
Respons.cs
namespace Survey.Models
{
using System;
using System.Collections.Generic;
public partial class Respons
{
public int ResponseID { get; set; }
public string Username { get; set; }
public string Name { get; set; }
public string EmailAddress { get; set; }
public Nullable<System.DateTime> DateTime { get; set; }
}
}
原始查询
DbSqlQuery<Respons> Responses = DbContext.Responses.SqlQuery("SELECT max(ResponseID) AS ResponseID, COUNT(CreatedDateTime) AS Total, MAX(FORMAT(CreatedDateTime, 'dd-MMM-yyyy', 'en-US' )) as Date FROM Responses GROUP BY CAST(CreatedDateTime AS DATE) ORDER BY Date DESC");
foreach ( var r in Responses ){
Response.Write( r.TotalCount);
}
先做一个ViewModel
class如下:
public class TotalCountByDateViewModel
{
public int TotalCount { get; set; }
public string Date { get; set; }
}
现在按如下方式编写您的查询:
DbSqlQuery<TotalCountByDateViewModel> totalCountsByDate = DbContext.Responses
.SqlQuery("SELECT max(ResponseID) AS ResponseID, COUNT(CreatedDateTime)
AS Total, MAX(FORMAT(CreatedDateTime, 'dd-MMM-yyyy', 'en-US' ))
as Date FROM Responses GROUP BY CAST(CreatedDateTime AS DATE) ORDER BY Date DESC");
嗯,从消息中可以很清楚地看出您的 class 丢失了 "Total count" 属性。
group by query的写法参考这个link:
我不擅长 Entity Framework,但计划 运行 原始查询。现在我坚持这个。使用以下代码时,出现以下错误。
Error CS1061 'Respons' does not contain a definition for 'TotalCount' and no extension method 'Total' accepting a first argument of type 'Respons' could be found (are you missing a using directive or an assembly reference?)
当我 运行 在 SSMS 中进行相同的查询时,我得到了以下结果。
Respons.cs
namespace Survey.Models
{
using System;
using System.Collections.Generic;
public partial class Respons
{
public int ResponseID { get; set; }
public string Username { get; set; }
public string Name { get; set; }
public string EmailAddress { get; set; }
public Nullable<System.DateTime> DateTime { get; set; }
}
}
原始查询
DbSqlQuery<Respons> Responses = DbContext.Responses.SqlQuery("SELECT max(ResponseID) AS ResponseID, COUNT(CreatedDateTime) AS Total, MAX(FORMAT(CreatedDateTime, 'dd-MMM-yyyy', 'en-US' )) as Date FROM Responses GROUP BY CAST(CreatedDateTime AS DATE) ORDER BY Date DESC");
foreach ( var r in Responses ){
Response.Write( r.TotalCount);
}
先做一个ViewModel
class如下:
public class TotalCountByDateViewModel
{
public int TotalCount { get; set; }
public string Date { get; set; }
}
现在按如下方式编写您的查询:
DbSqlQuery<TotalCountByDateViewModel> totalCountsByDate = DbContext.Responses
.SqlQuery("SELECT max(ResponseID) AS ResponseID, COUNT(CreatedDateTime)
AS Total, MAX(FORMAT(CreatedDateTime, 'dd-MMM-yyyy', 'en-US' ))
as Date FROM Responses GROUP BY CAST(CreatedDateTime AS DATE) ORDER BY Date DESC");
嗯,从消息中可以很清楚地看出您的 class 丢失了 "Total count" 属性。
group by query的写法参考这个link: