Database Reference
In-Depth Information
foreach (var emp in emps)
{
Console.WriteLine("Employee: {0}, Age: {1}", emp.Name,
emp.Age.ToString());
}
}
}
}
public class MyFunctions
{
[EdmFunction("EFRecipesModel", "FullName")]
public static string FullName(Employee employee)
{
throw new NotSupportedException("Direct calls are not supported.");
}
[EdmFunction("EFRecipesModel", "Age")]
public static int Age(Employee employee)
{
throw new NotSupportedException("Direct calls are not supported.");
}
}
The output of the code from Listing 11-6 is as follows:
Query using eSQL
Employee: Jill Robins, Age: 37
Employee: Michael Kirk, Age: 28
Employee: Karen Stanford, Age: 50
Query using LINQ
Employee: Jill Robins, Age: 37
Employee: Michael Kirk, Age: 28
Employee: Karen Stanford, Age: 50
How It Works
Our model-defined functions return types Edm.String for the FullName() function and Edm.Int32 for the Age()
function. These functions are defined on the conceptual level, so they don't directly refer to any type system outside of
the Entity Data Model's type system. These primitive types are easily translated to the CLR type system.
In the <DefiningExpression> or body of the model-defined functions, we directly access the properties of the
entities we received in the parameters. There is no need to use a select statement. However, the resulting expression
must have a type that matches the type defined as the return type of the function.
After inserting a few employees into our model, we first query using eSQL. We construct an eSQL expression that
invokes our two model-defined functions and projects the results to the Name and Age columns. Our eSQL expression
results in a collection of anonymous types that contain just the Name and Age members. Because we're not returning
one of the types defined in the model, we declare the type in CreateQuery<T>() to be DbDataRecord . We iterate over
the collection resulting from the evaluation of the query and print out the employees' names and ages.
 
Search WWH ::




Custom Search