Check for existing relationship between tables

Answered Check for existing relationship between tables

  • Thursday, June 19, 2008 9:35 AM
     
     
    Hi,

    I'm finding myself more and more just trying to check if a relationship exists i.e. in this example whether a staff member is a manager. I'm putting everything in try and catch blocks but I always thought that try and catch blocks were for unusual activity only. How can I test to see if a staff member is a manager without using try and catch blocks?

    What's the most effective way to do a test to see if a record exists with a binding?

    Thanks,
     
    Code Snippet

        var staff = db.Staffs.Single(st => st.Managers != null && st.StaffId =  IDprovided);

All Replies

  • Thursday, June 19, 2008 11:35 AM
     
     Answered
    Why don't you use SingleOrDefault and then test is staff is null

    var staff = db.Staffs.SingleOrDefault(st => st.Managers != null && st.StaffId =  IDprovided);

    if(staff == null)
    {
    ...

    }
    else
    {
    ....
    }
  • Thursday, June 19, 2008 12:03 PM
     
     
    Knew there must of been a simple answer!

    Thanks