Linq2DB向查询添加不必要的语句
我正在尝试生成一个查询,该查询获取每个给定月份内出现日期的条目数。我希望生成的查询的SQL形式如下所示:Linq2DB向查询添加不必要的语句,linq,linq-to-sql,linq2db,Linq,Linq To Sql,Linq2db,我正在尝试生成一个查询,该查询获取每个给定月份内出现日期的条目数。我希望生成的查询的SQL形式如下所示: SELECT DatePart(Year, [t1].[StartTime]) as Year, DatePart(Month, [t1].[StartTime]) as Month, Count(*) as 'Visits', Sum([t1].[PageCount]) as 'Total Pageviews' FROM [MyDatabase] [t1] GROUP BY DatePar
SELECT
DatePart(Year, [t1].[StartTime]) as Year,
DatePart(Month, [t1].[StartTime]) as Month,
Count(*) as 'Visits',
Sum([t1].[PageCount]) as 'Total Pageviews'
FROM
[MyDatabase] [t1]
GROUP BY
DatePart(Year, [t1].[StartTime]),
DatePart(Month, [t1].[StartTime])
我尝试了以下Linq2DB代码:
var query = from table in dataContext.MyDataContext(tablePath)
group table by new { table.StartTime.Year, table.StartTime.Month} into grp
select new { Month = grp.Key.Month, Year = grp.Key.Year,
TotalVisitors = grp.Count(),
TotalPageviews = grp.Sum(table2 => table2.PageCount) };
但由此生成的SQL查询是
-- SqlServer.2008 --
SELECT
[t1].[StartTime],
Count(*) as [c1],
Sum([t1].[PageCount]) as [c2]
FROM
[MyDatabase] [t1]
GROUP BY
DatePart(Year, [t1].[StartTime]),
DatePart(Month, [t1].[StartTime]),
[t1].[StartTime]
为什么是[t1].[StartTime]而不是月份或年份?为什么它在最后会被额外的[t1].[StartTime]分组?如何使用Linq2DB生成上面的SQL查询 请将以下解决方法与Sql.AsSql函数结合使用。我们正在努力解决这个长期存在的问题
var query = from table in dataContext.MyDataContext(tablePath)
group table by new { Sql.AsSql(table.StartTime.Year), Sql.AsSql(table.StartTime.Month)} into grp
select new { Month = Sql.AsSql(grp.Key.Month), Year = Sql.AsSql(grp.Key.Year),
TotalVisitors = grp.Count(),
TotalPageviews = grp.Sum(table2 => table2.PageCount) };