[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 251
  • Last Modified:

LINQ

I am trying to group by date in LINQ Query but its considering time also.
I want to group by only with '2011-04-15' instead of '2011-04-15 17:41:44.000' i.e excluding time part.

the query i am trying is:

 var getStartDateWithEventID = from e in contextObject.Events    
//where e.VenueId == Convert.ToInt32(VenueId) && e.StartDate <= DateTime.Now.Date || e.EndDate <= DateTime.Now.Date                                      
where e.VenueId == Convert.ToInt32(VenueId) && (e.StartDate.Month <= DateTime.Now.Month && e.StartDate.Day <= DateTime.Now.Day && e.StartDate.Year <= DateTime.Now.Year) || (e.EndDate.Month <= DateTime.Now.Month && e.EndDate.Day <= DateTime.Now.Day && e.EndDate.Year <= DateTime.Now.Year)

                                              group e by e.StartDate into g
                                              select new { StartDate = g.Key, NoOfEvents = g.Count() };

the value of startdate is:

2011-04-15 17:41:44.000
2011-04-15 17:51:49.000
2011-04-15 17:51:58.000
2011-04-17 17:41:44.000
2011-04-15 17:41:44.000
2011-04-15 17:41:44.000
2011-04-15 17:41:44.000
2011-04-15 17:41:44.000
2011-04-15 17:51:49.000
2011-04-15 17:51:58.000
2011-04-17 17:41:44.000
2011-04-15 17:41:44.000
2011-04-15 17:41:44.000
2011-04-15 17:41:44.000
2011-04-15 17:51:49.000
0
Dinesh Kumar
Asked:
Dinesh Kumar
  • 2
  • 2
2 Solutions
 
MicMaticCommented:
try using: startdate.Date
0
 
Dinesh KumarAuthor Commented:
Hi, it seems to be working..

is my where condition  related with date  in the question correct? or should I use the following:

where e.VenueId == Convert.ToInt32(VenueId) && e.StartDate <= DateTime.Now.Date || e.EndDate <= DateTime.Now.Date
0
 
MicMaticCommented:
I would use:

where e.VenueId == Convert.ToInt32(VenueId) && (e.StartDate.Date <= DateTime.Now.Date || e.EndDate.Date <= DateTime.Now.Date)
0
 
Dinesh KumarAuthor Commented:
Thanks.
0

Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now