views:

39

answers:

1

Hi, I use stored procedures in Entity Framework. Let's say that I have that procedure:

    public static int DoSth(decimal id)
    {
        return (int)Adapter.my_proc(id).First();
    }

as I don't want to get the First() element and then cast it as (int) everytime I'd like to have an extension method which do that for me (gets the First element and then cast it as a T). How will it looks like ?

+1  A: 

I'm not sure I understand what is your problem. Here is the code for simple extension method that takes ObjectResult<T>, gets first element of collections and cast it to T (put the method in separate static class).

public static T Fetch<T>(this ObjectResult<T> result)
{
    return (T)result.First();
}

And then you can call it like:

public static int DoSth(decimal id)
{
    return Adapter.my_proc(id).Fetch<int>();
}

Is that what you're thinking of?

A.
I've tried that already, but in that extension method the compiler doesn't know what the "T" is
Tony
Sure, it doesn't in previous version. Code edited, now it should work.
A.