Entity framework DateTime 到月、日和小时
Entity framework DateTime to month, day and hour
我有这个查询 returns 变量 date_showx as dateTime
var myList = (from p in db.Full
group p by p.date_reception into g
orderby g.Key
select new
{
date_showx = g.Key,
countx = g.Count()
}
).take(10).ToList();
在我的应用程序中,我有一个频率按钮,其值为月、日和小时,我需要我的查询结果匹配该类型,例如点击月份我只需要显示月份和年份,点击日期仅显示日期和月份,点击小时仅显示小时和日期,例如 Google Analytics one。
您的问题有点不清楚,但我认为您想要实现的是获取 collection 中项目的计数,而不是按确切日期分组,而是按部分日期分组。因此,例如,您的第一个要求是 "to show only month and year",这意味着您希望将您的物品分成每个月 + 年之后标记的袋子,并在每个袋子中进行计数。
这意味着您需要根据不止一件事进行分组。不是整个日期,而是日期内的多个属性。请参阅月 + 年的示例,我相信您将能够弄清楚其余部分。你也可以参考这个答案:C# Linq Group By on multiple columns
var list = new List<DateTime>
{
new DateTime(2014, 2, 1),
new DateTime(2014, 2, 13),
new DateTime(2014, 2, 22),
new DateTime(2014, 4, 1),
new DateTime(2014, 4, 3),
new DateTime(2014, 7, 1),
new DateTime(2014, 7, 6),
new DateTime(2014, 7, 6),
new DateTime(2015, 2, 1),
new DateTime(2015, 2, 1),
new DateTime(2015, 4, 2),
new DateTime(2015, 4, 1),
new DateTime(2015, 4, 1),
};
var myList = (from p in list
group p by new {p.Month, p.Year} into g
orderby g.Key.Year, g.Key.Month
select new
{
Year = g.Key.Year,
Month = g.Key.Month,
Count = g.Count()
}
).Take(10).ToList();
foreach (var item in myList)
{
Console.WriteLine("Year: {0}, Month: {1}, Count {2}", item.Year, item.Month, item.Count);
}
Console.ReadKey();
我有这个查询 returns 变量 date_showx as dateTime
var myList = (from p in db.Full
group p by p.date_reception into g
orderby g.Key
select new
{
date_showx = g.Key,
countx = g.Count()
}
).take(10).ToList();
在我的应用程序中,我有一个频率按钮,其值为月、日和小时,我需要我的查询结果匹配该类型,例如点击月份我只需要显示月份和年份,点击日期仅显示日期和月份,点击小时仅显示小时和日期,例如 Google Analytics one。
您的问题有点不清楚,但我认为您想要实现的是获取 collection 中项目的计数,而不是按确切日期分组,而是按部分日期分组。因此,例如,您的第一个要求是 "to show only month and year",这意味着您希望将您的物品分成每个月 + 年之后标记的袋子,并在每个袋子中进行计数。 这意味着您需要根据不止一件事进行分组。不是整个日期,而是日期内的多个属性。请参阅月 + 年的示例,我相信您将能够弄清楚其余部分。你也可以参考这个答案:C# Linq Group By on multiple columns
var list = new List<DateTime>
{
new DateTime(2014, 2, 1),
new DateTime(2014, 2, 13),
new DateTime(2014, 2, 22),
new DateTime(2014, 4, 1),
new DateTime(2014, 4, 3),
new DateTime(2014, 7, 1),
new DateTime(2014, 7, 6),
new DateTime(2014, 7, 6),
new DateTime(2015, 2, 1),
new DateTime(2015, 2, 1),
new DateTime(2015, 4, 2),
new DateTime(2015, 4, 1),
new DateTime(2015, 4, 1),
};
var myList = (from p in list
group p by new {p.Month, p.Year} into g
orderby g.Key.Year, g.Key.Month
select new
{
Year = g.Key.Year,
Month = g.Key.Month,
Count = g.Count()
}
).Take(10).ToList();
foreach (var item in myList)
{
Console.WriteLine("Year: {0}, Month: {1}, Count {2}", item.Year, item.Month, item.Count);
}
Console.ReadKey();