我想在没有(小时和分钟和秒)的情况下使用GroupBy for DateTime [英] I want to use GroupBy for DateTime Without (Hour and Minute and Second)

查看:132
本文介绍了我想在没有(小时和分钟和秒)的情况下使用GroupBy for DateTime的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在没有(小时和分钟和秒)的情况下使用GroupBy。

我只想使用年,月和日...

我怎么样使用MethodBased Linq进行这项工作。

假设我的表格如下

I want to use GroupBy for DateTime Without (Hour and Minute and Second).
I want to use only Year and Month and Day...
how I use MethodBased Linq for this work.
suppose My table Like below

ViewDate
3/3/2012 01:03:12
1/1/2012 10:43:23
1/2/2012 09:12:12
3/3/2012 09:43:13
1/1/2012 09:14:19
1/2/2012 01:15:08
1/2/2012 08:10:23
3/3/2012 12:15:08
3/3/2012 14:43:14
1/2/2012 09:33:08
1/1/2012 18:43:12
1/2/2012 09:38:18
1/1/2012 17:43:15
1/2/2012 17:48:16
1/2/2012 06:53:14
1/2/2012 01:12:08
1/1/2012 14:01:16
1/2/2012 12:02:16



我想要低于结果


I want below result

ViewDate   count
1/1/2012    5
1/2/2012    9
3/3/2012    4

推荐答案

试试这种方式

Try this way
List <content>= dbcontext.content.ToList().Where(u =>(Convert.ToDateTime(u.Date).Month==M_no) || (Convert.ToDateTime(u.Date).Year==year_no) || (Convert.ToDateTime(u.Date).Day==day_no)).ToList(); 
</content>



示例将演示属性。

System.DateTime moment = new System.DateTime(2000, 1,15,3,57,32,11);

//年份获得2000.

int year = moment.Year;

//月份获得1(1月)。

int month = moment.Month;

//天获得15.

int day = moment.Day;





谢谢


example will demonstrate the properties .
System.DateTime moment = new System.DateTime(2000, 1, 15, 3, 57, 32, 11);
// Year gets 2000.
int year = moment.Year;
// Month gets 1 (January).
int month = moment.Month;
// Day gets 15.
int day = moment.Day;


thanks


我解决了它

I solved it
var q = _db.ArticleViews.Where(m => m.ArticleId == ArticleId).GroupBy(m => new { m.ViewDate.Year,m.ViewDate.Month,m.ViewDate.Day }, (ViewDate, ArticleId1) => new
            {
                ViewDate,
                CountInDate = ArticleId1.Count(),
            });


这篇关于我想在没有(小时和分钟和秒)的情况下使用GroupBy for DateTime的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆