PrepAway - Latest Free Exam Questions & Answers

How do you call a model-defined function as static method on a custom class?

How do you call a model-defined function as static method on a custom class?

PrepAway - Latest Free Exam Questions & Answers

A.
Add a class to your application with a static method that does the following:
Apply an EdmFunctionAttribute to the method and ensure it accepts an IQueryable argument
and returns the results of the Execute method that is returned by the Provider property.

B.
Add a class to your application with a static method that does the following:
Apply an EdmFunctionAttribute to the method and ensure it accepts IEntityWithRelationships argument
and returns the results of the Execute method that is returned by the Provider property.

C.
Add a class to your application with a static method that does the following:
Apply an EdmFunctionAttribute to the method and ensure it accepts ICollection argument
and returns the results of the Execute method that is returned by the Provider property.

D.
Add a class to your application with a static method that does the following:
Apply an EdmFunctionAttribute to the method and ensure it accepts
and returns the results of the Execute method that is returned by the Provider property.

Explanation:
To call a model-defined function as static method on a custom class:
1.Add a class to your application with a static method that does the following:
– Maps to the function defined in the conceptual model. To map the method, you must apply an EdmFunctionAttribute to the method.
Note that the NamespaceName and FunctionName parameters of the attribute are the namespace name of the conceptual model and the function name in the conceptual model, respectively.
– Accepts an IQueryable argument.
– Returns the results of the Execute method that is returned by the Provider property.
2.Call the method as a member a static method on the custom class

Example:
– function mapping
<Function Name=”GetDetailsById”
ReturnType=”Collection(AdventureWorksModel.SalesOrderDetail)”>
<Parameter Name=”productID” Type=”Edm.Int32″ />
<DefiningExpression>
SELECT VALUE s
FROM AdventureWorksEntities.SalesOrderDetails AS s
WHERE s.ProductID = productID
</DefiningExpression>
</Function>
– source code
public partial class AdventureWorksEntities : ObjectContext
{
[EdmFunction(“AdventureWorksModel”, “GetDetailsById”)]
public IQueryable<SalesOrderDetail> GetDetailsById(int productId)
{
return this.QueryProvider.CreateQuery<SalesOrderDetail>(Expression.Call(
Expression.Constant(this),
(MethodInfo)MethodInfo.GetCurrentMethod(),
Expression.Constant(productId, typeof(int))));
}
}

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

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

One Comment on “How do you call a model-defined function as static method on a custom class?

  1. Gaius says:

    The example given was for an instance method in an object context subclass. This is for a static method in a custom class:

    public class MyClass
    {
    [EdmFunction(“AdventureWorksModel”, “GetProductRevenue”)]
    public static decimal? GetProductRevenue(IQueryable details)
    {
    return details.Provider.Execute( Expression.Call((MethodInfo)MethodInfo.GetCurrentMethod(), Expression.Constant(details, typeof(IQueryable))));
    }
    }




    0



    0

Leave a Reply