user1662812
user1662812

Reputation: 2601

How to pass instance method as a delegate

I have this method that I use with EF

RetryGetWithExpression<city, List<city>>(u => u.Take(10).ToList());

public static TValue RetryGetWithExpression<T, TValue>(
        Func<ObjectSet<T>, TValue> func)
    where T : class
{
    var entitySet = entitiesContext.CreateObjectSet<T>();
    return func(entitySet);
}

The above code works with the table entities generated by EF. What I am trying to do is to change the code in such way that it works with stored procedures.

This is the code that executes the SP:

Entities G = new Entities();
ObjectResult<retrieveMedia_Result> F = G.retrieveMedia(1);

When I try to convert RetryGetWithExpression to accept SPs, I get a problem that retrieveMedia is instance method and I cant pass it as u.retrieveMedia(1)

RetryGetWithExpression<Entities, ObjectResult<retrieveMedia_Result>>(
    u => u.retrieveMedia(1));

public static TValue RetryGetWithExpression<T, TValue>(
        Func<ObjectSet<T>, TValue> func)
    where T : class
{

}

How to change the above code so that It works with SPs?

Upvotes: 0

Views: 243

Answers (2)

Jodrell
Jodrell

Reputation: 35716

Somthing like,

public static TValue RetryGetWithExpression<TParam, TValue>(
        Func<TParam, TValue> func,
        TParam parameter)
{
    return func(parameter);
}

Which you would call like,

Entities g = new Entities();
ObjectResult<retrieveMedia_Result> f = 
    RetryGetWithExpression<int?, ObjectResult<retrieveMedia_Result>>(
        g.retrieveMedia,
        1);

but, it seems pretty pointless, because all you want to do is pass a function delegate and call it.

Upvotes: 1

Eranga
Eranga

Reputation: 32447

Try

RetryGetWithExpression<ObjectResult<retrieveMedia_Result>>(u => u.retrieveMedia(1));

public static TValue RetryGetWithExpression<TValue>(Func<Entities, TValue> func)
{
    return func(entitiesContext);
}

Upvotes: 1

Related Questions