none
DefaultIfEmpty - Test for empty result set? RRS feed

  • Question

  • I've used DefaultIfEmpty in a simple Linq query like this:

    var test = dbc.TestData.Where(t => t.Test_id == testId).DefaultIfEmpty();
    

    I want to perform different actions depending on whether the result set returns any row(s) or no rows at all, how do I properly check for this?  Checking if test == null doesn't appear to work.

     

    Tuesday, November 29, 2011 3:27 PM

Answers

  • Hi jabberpunch;

    It can be tested like this.

    if ( test.Count( ) == 1 && test.First( ) == null )
    {
        // The collection in Test is empty
    }
    else
    {
        // The collection in Test has value/s
    }
    

     


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    • Marked as answer by jabberpunch Tuesday, November 29, 2011 9:11 PM
    Tuesday, November 29, 2011 4:01 PM

All replies

  • Hi jabberpunch;

    It can be tested like this.

    if ( test.Count( ) == 1 && test.First( ) == null )
    {
        // The collection in Test is empty
    }
    else
    {
        // The collection in Test has value/s
    }
    

     


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    • Marked as answer by jabberpunch Tuesday, November 29, 2011 9:11 PM
    Tuesday, November 29, 2011 4:01 PM
  • Hi Fernando,

    I tried using test.Count() but not in conjunction with test.First()...does that mean DefaultIfEmpty will always return at least one object but that object is empty or contains all null properties?  Thanks for the help, much appreciated.

    Tuesday, November 29, 2011 9:13 PM
  • Hi jabberpunch;

    The documentation states that the method DefaultIfEmpty() will "Returns the elements of an IEnumerable<T>, or a default valued singleton collection if the sequence is empty.". So to your question, "does that mean DefaultIfEmpty will always return at least one object but that object is empty or contains all null properties?", That depends on where it is used. For example if you were to use it in a Linq to SQL and the return type is an object of a reference type then it will return a collection of that type 1 or more elements or just one element set to null if no object where to be return. If the return type is of a value type such as integer then it will throw an exception. In the case of something like Linq to Object then It will return a collection of int's. If there were no objects selected from the query then the collection will have one element in it and its value will contain the default value for the type being returned where in this case it was int it would return a collection of one element with its value set to zero.

     


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    Tuesday, November 29, 2011 10:00 PM
  • Hi,

    Thank you all for your active participation in the MSDN forum!

    Best Regards


    Allen Li [MSFT]
    MSDN Community Support | Feedback to us
    Thursday, December 1, 2011 2:52 AM
    Moderator