PrepAway - Latest Free Exam Questions & Answers

You need to add a function that returns the number of years since a person was hired

You use Microsoft Visual Studio 2010 and Microsoft .NET Framework 4 to create an application. The
application uses the ADO.NET Entity Framework to model entities. The model includes the entity
shown in the following exhibit.

You need to add a function that returns the number of years since a person was hired. You also need
to ensure that the function can be used within LINQ to Entities queries. What should you do?

PrepAway - Latest Free Exam Questions & Answers

A.
Add the following code fragment to the .edmx file.
<Function Name=”YearsSinceNow” ReturnType=”Edm.Int32″>
<Parameter Name=”date” Type=”Edm.DateTime” />
<DefiningExpression>
Year(CurrentDateTime()) – Year(date)
</DefiningExpression>
</Function>
Add the following function to the entity class definition.
<EdmComplexType(“SchoolModel”, “YearsSinceNow”)> _
Public Shared Function YearsSinceNow([date] As DateTime) As Integer
Throw New NotSupportedException(“Direct calls are not supported.”)
End Function

B.
Add the following code fragment to the .edmx file.
<Function Name=”YearsSinceNow” ReturnType=”Edm.Int32″>
<Parameter Name=”date” Type=”Edm.DateTime” />
<DefiningExpression>
Year(CurrentDateTime()) – Year(date)
</DefiningExpression>
</Function>
Add the following function to the entity class definition.
<EdmFunction(“SchoolModel”, “YearsSinceNow”)> _
Public Shared Function YearsSinceNow([date] As DateTime) As Integer
Throw New NotSupportedException(“Direct calls are not supported.”)
End Function

C.
Add the following code fragment to the .edmx file.
<Function Name=”YearsSinceNow” ReturnType=”Edm.Int32″>
<Parameter Name=”date” Type=”Edm.DateTime” />
</Function>
Add the following function to the entity class definition.
<EdmFunction(“SchoolModel”, “YearsSinceNow”)> _

Public Shared Function YearsSinceNow([date] As DateTime) As Integer
Return Year(CurrentDateTime() – Year([date]))
End Function

D.
Use the Entity Data Model Designer to create a complex property named YearsSinceNow that can
be accessed through the LINQ to Entities query at a later time.

Explanation:
How to: Call Model-Defined Functions in Queries
(http://msdn.microsoft.com/en-us/library/dd456857.aspx)
How to: Call Model-Defined Functions as Object Methods
(http://msdn.microsoft.com/en-us/library/dd456845.aspx)


Leave a Reply