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?

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?

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 static int YearsSinceNow(DateTime date)
{
throw new NotSupportedException(“Direct calls are not supported.”);
}

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 static int YearsSinceNow(DateTime date)
{
throw new NotSupportedException(“Direct calls are not supported.”);
}

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 static int YearsSinceNow(DateTime date)
{
return Year(CurrentDateTime() – Year(date));
}

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 0

Your email address will not be published. Required fields are marked *