locked
A Function Returning Two Query Results RRS feed

  • Question

  • Hi Everyone,

    I have been wondering how or what is the best way to return two results from a function. As context what I do in the function is equivalently to make two queries (I just think of them as two tables), I then add them together and return the result. I was making the application and thought it would be nice if I returned one of the intermediate results and the final one.

    Then I thought how? Should I make my own class with two parts base (the intermediate result) and then result (which is the result). Or some sort of collections or something. If I was doing this with SQL rather than Linq I would just return a dataset with two datatables.

    Any Advice?

    Thanks,
    Anthony
    Monday, September 21, 2009 8:49 AM

Answers

  • This won't help you now, but in 4.0, Tuple's and in fact, anonymous return types will be the way to go. You can find 2.0/3.5 Tuple implementations out on the interwebs, (http://elevate.codeplex.com/ for example). Tuples are heterogeneous collections, static size, static typed. In the mean time I've often resorted to returning a straight-up array (if the return types are the same) and just document the meaning of the indexed elements, or as you also suggested, write a small custom (sometimes inner) struct/class that contains two or more properties that represent the return values.

    Of course, you could also use "out" parameters ... yuck ;)

    Chris
    • Marked as answer by Zhipeng Lee Monday, September 28, 2009 1:21 AM
    Monday, September 21, 2009 6:33 PM

All replies

  • This won't help you now, but in 4.0, Tuple's and in fact, anonymous return types will be the way to go. You can find 2.0/3.5 Tuple implementations out on the interwebs, (http://elevate.codeplex.com/ for example). Tuples are heterogeneous collections, static size, static typed. In the mean time I've often resorted to returning a straight-up array (if the return types are the same) and just document the meaning of the indexed elements, or as you also suggested, write a small custom (sometimes inner) struct/class that contains two or more properties that represent the return values.

    Of course, you could also use "out" parameters ... yuck ;)

    Chris
    • Marked as answer by Zhipeng Lee Monday, September 28, 2009 1:21 AM
    Monday, September 21, 2009 6:33 PM
  • Cheers. Thanks for the information, I'll have a look at the link you sent.

    Is this the general consensus?

    At the moment I have made a custom class for the output and got on with things but I am interested in how people do this in general and or if they do it (return several things) from one function?

    Thanks again.
    Anthony
    Tuesday, September 22, 2009 7:03 AM