Database Reference
In-Depth Information
var records =
((IObjectContextAdapter)context).ObjectContext.CreateQuery<DbDataRecord>(esql);
Console.WriteLine("Events by State and City...");
foreach (var rec in records)
{
Console.WriteLine("{0}, {1}", rec["City"], rec["State"]);
var events = (List<Event>)rec["Events"];
foreach (var ev in events)
{
Console.WriteLine("\t{0}", ev.Name);
}
}
}
Following is the output of the code in Listing 3-33:
Using LINQ
Events by State and City...
Louisville, KY
Thunder on the Ohio
Raytown, MO
Little Blue River Festival
Fourth of July Fireworks
Dallas, TX
TechFest 2010
BBQ Ribs Championship
Using Entity SQL
Events by State and City...
Louisville, KY
Thunder on the Ohio
Raytown, MO
Little Blue River Festival
Fourth of July Fireworks
Dallas, TX
TechFest 2010
BBQ Ribs Championship
How It Works
In Listing 3-33, we show two different approaches to the problem. The first approach uses LINQ and the group by
operator to group the results by state and city. When using the group by operator for multiple properties, we create an
anonymous type to initially group the data. We use an into clause to send the groups to g , which is a second sequence
created to hold the results of the query.
We project the results from g into a second anonymous type getting the State from the group key's State field
(from the first anonymous type) and the City from the group key's City field. For the events, we simply select all of the
members of the group.
For the Entity SQL approach, we can only project columns used in the group by clause, a constant value, or a
computed value from using an aggregate function. In our case, we project the state, city, and the collection of events
for each grouping.
 
Search WWH ::




Custom Search