开发者

C# Extension method for ObjectResult<T>?

I use stored procedures in Entity Framework. L开发者_运维知识库et'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 ?


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?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜