Click here to Skip to main content
15,886,799 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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
Posted
Updated 8-Sep-14 5:50am
v3

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>

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
 
Share this answer
 
Comments
kave2011 8-Sep-14 11:43am    
I improved question
I solved it
C#
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(),
            });
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900