Database Reference
In-Depth Information
11-2. Filtering an Entity Collection Using a
Model-Defined Function
Problem
You want to create a model-defined function that filters a collection.
Solution
Suppose that we have a model with Customers and Invoices, as shown in Figure 11-2 .
Figure 11-2. Customer and Invoice in a model
Let's say that we want to create a model-defined function that takes a collection of invoices and filters the
collection to those invoices that have an amount greater than $300. Just for fun, let's use this model-defined function
in a query that further filters this collection to just those invoices created after 5/1/2013. Of course, we'll want to load
all of the customers associated with these invoices.
To get started, do the following:
1.
Right-click the .edmx file in the Solution Explorer, and select Open With XML Editor.
2.
Insert the code in Listing 11-3 just below the <Schema> tag in the conceptual models
section of the .edmx file. This defines the function in the model.
Listing 11-3. The GetInvoices() Model-Defined Function
<Function Name="GetInvoices" ReturnType="Collection(EFRecipesModel.Invoice)" >
<Parameter Name="invoices" Type="Collection(EFRecipesModel.Invoice)">
</Parameter>
<DefiningExpression>
Select VALUE i
from invoices as i where i.Amount > 300M
</DefiningExpression>
</Function>
3.
Insert into and query the model using code similar to the pattern shown in Listing 11-4.
 
Search WWH ::




Custom Search