How do I check if a ISIngleResult<T> contains 0 items RRS feed

  • Question

  • Hello all,

       I want to know if a ISingleResult<T> is empty or not (it is the result of a call to a SP).

    I have tried the two following methods:

    1. Check count:

    if (result.Count() == 0)

    2. Check if I can get any item:

    if (result.FirstOrDefault() == null)

    Both methods are bad because later when I do:

    foreach (var item in result)

    I get the exception:
    "The query results cannot be enumerated more than once."

    How do I do than?

    Thanks in advance,
    • Edited by Keren S Wednesday, February 4, 2009 3:24 PM
    Wednesday, February 4, 2009 11:57 AM


All replies

  • Why don't you do something like
    var result = dataContext.Table.SingleOrDefault();
    if(result != default(TableType))
    And also, if you are getting a single result, why do you need to iterate through them?

    Daniel -
    Wednesday, February 4, 2009 3:15 PM
  • Hi Daniel!

    1. I am not working with tables, I am working with StoredProcedures. I will edit and adjust my quetion, it was probably not clear.

    2. ISingleResult means that the SP returns the result of only one select sentance. A result of a single select statement is often plural lines. Those line are what I am iterating between.

    Wednesday, February 4, 2009 3:23 PM
  • Ah, that makes more sense.  Have you tried making the results a normal List<T> instead?
    Daniel -
    Wednesday, February 4, 2009 3:40 PM
  •  Don't you think it sounds like way too much work to turn the whole result to a List<T> just in order to check how many items are on it? :-) Seems to me like there must be a more simple way :-)
    Wednesday, February 4, 2009 3:43 PM
  • All you have to do is result.ToList();
    Daniel -
    • Marked as answer by Keren S Thursday, February 5, 2009 9:03 AM
    Wednesday, February 4, 2009 3:57 PM
  • Hi Daniel,

       Now I got what you meant: Not use ISingleResult at all but turn it into a List and use only the list.
    I did that. Works perfektly! Thanks.

    All the best
    Thursday, February 5, 2009 9:04 AM