Database Reference
In-Depth Information
Note that the Entity SQL statement in listing 3-8 uses the value keyword. This keyword is useful when we need
to fetch the entire entity. If our Entity SQL statement projected a specific subset of columns (that is, we use some of
the columns and/or create columns using Entity SQL expressions), we can dispense with the value keyword. When
working with a context object, this means working with a DbDataRecord directly as demonstrated in Listing 3-9.
Listing 3-9. Projecting with Both Object Services and EntityClient
// using object services without the VALUE keyword
using (var context = new EFRecipesEntities())
{
Console.WriteLine("Customers...");
string esql = "select c.Name, c.Email from Customers as c";
var records = context.CreateQuery<DbDataRecord>(esql);
foreach (var record in records)
{
var name = record[0] as string;
var email = record[1] as string;
Console.WriteLine("{0}'s email is: {1}", name, email);
}
}
Console.WriteLine();
// using EntityClient without the VALUE keyword
using (var conn = new EntityConnection("name=EFRecipesEntities"))
{
Console.WriteLine("Customers...");
var cmd = conn.CreateCommand();
conn.Open();
cmd.CommandText = @"select c.Name, C.Email from
EFRecipesEntities.Customers as c";
using (var reader = cmd.ExecuteReader(CommandBehavior.SequentialAccess))
{
while (reader.Read())
{
Console.WriteLine("{0}'s email is: {1}",
reader.GetString(0), reader.GetString(1));
}
}
}
When you form a projection in Entity SQL, the results are returned in a DbDataRecord object that contains one
element for each column in the projection. With the value keyword, the single object resulting from the query is
returned in the first element of the DbDataRecord .
 
Search WWH ::




Custom Search