Database Reference
In-Depth Information
Listing 3-29. Grouping by the Date Portion of a DateTime Property
using (var context = new EFRecipesEntities())
{
context.Registrations.Add(new Registration {
StudentName = "Jill Rogers",
RegistrationDate = DateTime.Parse("12/03/2009 9:30 pm") });
context.Registrations.Add(new Registration {
StudentName = "Steven Combs",
RegistrationDate = DateTime.Parse("12/03/2009 10:45 am") });
context.Registrations.Add(new Registration {
StudentName = "Robin Rosen",
RegistrationDate = DateTime.Parse("12/04/2009 11:18 am") });
context.Registrations.Add(new Registration {
StudentName = "Allen Smith",
RegistrationDate = DateTime.Parse("12/04/2009 3:31 pm") });
context.SaveChanges();
}
using (var context = new EFRecipesEntities())
{
var groups = from r in context.Registrations
// leverage built-in TruncateTime function to extract date portion
group r by DbFunctions.TruncateTime(r.RegistrationDate)
into g
select g;
foreach (var element in groups)
{
Console.WriteLine("Registrations for {0}",
((DateTime)element.Key).ToShortDateString());
foreach (var registration in element)
{
Console.WriteLine("\t{0}", registration.StudentName);
}
}
}
Following is the output of the code in Listing 3-29:
Registrations for 12/3/2009
Jill Rogers
Steven Combs
Registrations for 12/4/2009
Robin Rosen
Allen Smith
Search WWH ::




Custom Search