none
Determining empty collection from a VB Linq to SQL query RRS feed

  • Question

  • Hi, noob here, please be gentle :-)

    I have defined a simple query to pull some data back and place into fields on a form.  All is going well when the data is there, however I want to detect if the returned object is empty.  The problem is how do determie if the object is empty???

    This is the query

     

    Dim empday = (From emp In db.EmployeeDays Where emp.LogonName = "BlahBLAH" And emp.DayDate = "1/1/2011" Select emp).FirstOrDefault

     

    I've read a lot stating that this should return a default value if no records are found, but how do I test for this?

    Many thanks in advance!

    Tuesday, May 4, 2010 10:31 PM

Answers

All replies

  • Try checking if empday is null (vb  "Nothing"?).
    • Marked as answer by barryw99 Friday, May 7, 2010 10:11 AM
    Wednesday, May 5, 2010 6:46 AM
  • Note that the documentation for FirstOrDefault says that it returns "default(TSource) if source is empty". For nullable types that means null (Nothing), for non-nullable types that would be something else.

    For example if your emp record has an integer column called "Standard hours" and you only selected that, it would return an int which is non-nullable and would get a default value of 0.

    Dim emphours = (From emp In db.EmployeeDays 
        Where emp.LogonName = "BlahBLAH" And emp.DayDate = "1/1/2011" 
        Select emp.StandardHours).FirstOrDefault
    

    Does that help?

    Wednesday, May 5, 2010 6:57 AM
  • Thanks for this John, but I am guessing that

    Select emp.StandardHours

     

    is only going to return that value??  Can I do the same test on one field if I just do Select emp ?

    Wednesday, May 5, 2010 7:16 PM
  • John, found it myself, wonders of a good night sleep!

    Very simple, this did it.  THanks again! 

    If empday Is Nothing Then
    • Marked as answer by barryw99 Wednesday, May 5, 2010 9:20 PM
    • Unmarked as answer by barryw99 Friday, May 7, 2010 10:11 AM
    Wednesday, May 5, 2010 9:18 PM
  • Thanks for this John, but I am guessing that Select emp.StandardHours is only going to return that value??

    The select is going to return a set of integers; one for each EmployeeDay record which matches the where clause (i.e. it will return zero or more records). The FirstOrDefault qualifies that to zero or one. An integer is not nullable, so checking "if emphours is Nothing" is meaningless, in that case you have to check the default value for that type (0 for an integer).

    So my point was that you have to know whether the return type is nullable or not to successfully do the check.

    Thursday, May 6, 2010 7:07 AM
  • John, found it myself, wonders of a good night sleep!

    Very simple, this did it.  THanks again! 

    If empday Is Nothing Then
    
    

     

    Wasn't that what I suggested in my first response?

    Thursday, May 6, 2010 7:08 AM
  • Sorry John, you are right.

    I will correct this

    Friday, May 7, 2010 10:11 AM