Database Reference
In-Depth Information
Listing 5-17. The Correct Placement of the Include Method When Applying Filtering and Grouping Expressions on
the Parent Entity
using (var context = new Recipes7Context())
{
var club = new Club {Name = "Star City Chess Club", City = "New York"};
club.Events.Add(new Event
{
EventName = "Mid Cities Tournament",
EventDate = DateTime.Parse("1/09/2010"), Club = club
});
club.Events.Add(new Event
{
EventName = "State Finals Tournament",
EventDate = DateTime.Parse("2/12/2010"), Club = club
});
club.Events.Add(new Event
{
EventName = "Winter Classic",
EventDate = DateTime.Parse("12/18/2009"), Club = club
});
context.Clubs.Add(club);
context.SaveChanges();
}
using (var context = new Recipes7Context())
{
var events = from ev in context.Events
where ev.Club.City == "New York"
group ev by ev.Club
into g
select g.FirstOrDefault(e1 => e1.EventDate == g.Min(evt => evt.EventDate));
var eventWithClub = events.Include("Club").First();
Console.WriteLine("The next New York club event is:");
Console.WriteLine("\tEvent: {0}", eventWithClub.EventName);
Console.WriteLine("\tDate: {0}", eventWithClub.EventDate.ToShortDateString());
Console.WriteLine("\tClub: {0}", eventWithClub.Club.Name);
}
The output of the code in Listing 5-17 is the following:
The next New York club event is:
Event: Winter Classic
Date: 12/18/2009
Club: Star City Chess Club
 
Search WWH ::




Custom Search