【发布时间】:2016-05-26 01:44:27
【问题描述】:
我的表中有此列。
Date
-----
2014-01-15
2014-05-03
.
.
我想使用 linq to Sql 运行这个查询:
SELECT
DATEPART(MONTH,date) as dateColumn
FROM
[mydatabase].[dbo].[global_currency_entries]
WHERE
date >= '2014-01-01' AND date<='2014-12-31'
GROUP BY
DATEPART(Month,date)
ORDER BY
DATEPART(MONTH,date)
我正在使用的 Linq to sql 代码:
var timePeriod = (from gce in db.global_currency_entries
where gce.date >= Convert.ToDateTime("2014-01-01") && gce.date <= Convert.ToDateTime("2014-12-31")
orderby gce.date.Month
group gce by new {gce.date.Month});
Orderby 在上述情况下不起作用。
foreach (var time in timePeriod)
{
lblOutput.Text = lblOutput.Text + time.Key + ",";
}
我看到了这个输出:
{ Month = 9 },{ Month = 3 },{ Month = 12 },{ Month = 6 },{ Month = 7 },{ Month = 1 },{ Month = 10 },{ Month = 4 },{ Month = 5 },{ Month = 2 },{ Month = 11 },{ Month = 8 },
【问题讨论】:
-
与 SQL 版本相比,您的组和排序子句颠倒了。为什么?
-
aaa 是我的错,,,我现在明白了,,,
-
正确的方法是
(from gce in db.global_currency_entries where gce.date >= Convert.ToDateTime("2014-01-01") && gce.date <= Convert.ToDateTime("2014-12-31") select gce.date.Month).GroupBy(p => p).OrderBy(p => p.Key);