none
How To Avoid Double Query RRS feed

  • Question

  • I am checking the Count() on my query before I call a Single<object>() on the query.  This results in two queries?  Can I just use SingleOrDefault() instead and check for null?
    Thursday, July 24, 2008 9:31 PM

Answers

  • If you use SingleOrDefault and return more than one record, you will get an exception. In that case, use FirstOrDefault. Either way, you should be able to use the method and check for null, thereby eliminating the Count method. As an alternative, you can cast the results .ToList and then work on the resulting list without the need for multiple queries.

     

    Jim Wooley

    www.ThinqLinq.com

     

    Friday, July 25, 2008 3:22 PM
    Moderator

All replies

  • Can you please post the query?

     

    Thanks,

     

    --Samir

     

    Thursday, July 24, 2008 9:35 PM
  • If you use SingleOrDefault and return more than one record, you will get an exception. In that case, use FirstOrDefault. Either way, you should be able to use the method and check for null, thereby eliminating the Count method. As an alternative, you can cast the results .ToList and then work on the resulting list without the need for multiple queries.

     

    Jim Wooley

    www.ThinqLinq.com

     

    Friday, July 25, 2008 3:22 PM
    Moderator