Database Reference
In-Depth Information
11-6. Returning a Complex Type from a Model-Defined Function
Problem
You want to return a complex type from a model-defined function.
Solution
Suppose that we have a model for patients and their visits to a local hospital. This model is shown in Figure 11-6 .
Figure 11-6. A model for patient visits
You want to create a model-defined function that returns summary information about the patient with their
name, the total number of visits, and their accumulated bill. Additionally, you want to filter the results to include only
patients over 40 years old.
To create and use the model-defined function, do the following:
1.
Right-click the designer, and select Add Complex Type.
2.
Right-click the new complex type in the Model Browser. Rename the type to
VisitSummary, and add the following properties:
a.
Name of type String, not nullable
b.
TotalVisits of type Int32, not nullable
c.
TotalCost of type Decimal, not nullable
3.
Right-click the .edmx file in the Solution Explorer, and click Open With XML Editor. This
will open the .edmx file in the XML Editor.
Insert the code in Listing 11-11 just below the <Schema> tag in the conceptual models
section of the .edmx file. This defines the function in the model.
4.
Listing 11-11. The GetVisitSummary() Model-Defined Function
<Function Name="GetVisitSummary" ReturnType="Collection(EFRecipesModel.VisitSummary)">
<DefiningExpression>
select VALUE EFRecipesModel.VisitSummary(pv.Patient.Name,
Count(pv.VisitId),Sum(pv.Cost))
from EFRecipesEntities.PatientVisits as pv
group by pv.Patient.PatientId
</DefiningExpression>
</Function>
 
Search WWH ::




Custom Search