how to know the record I want to query doesn't exist via LINQ?

Last post 08-13-2007 11:51 AM by ndinakar. 1 replies.

Sort Posts:

  • how to know the record I want to query doesn't exist via LINQ?

    08-13-2007, 4:45 AM
    • Loading...
    • Moosdau
    • Joined on 05-11-2007, 3:49 AM
    • China
    • Posts 48

    Hi all:

    for example:
    testDataContext db=new testDataContext();
    var res=db.tables.single(p=>p.columnName=="hi");

    if there is a record in the database, it works well, but if there isn't , it will throw an exception, then , How could I know the record exists or not ?  I don't think exception is a resonable way. and in my opinion, there should be--------even must be ------a resonable way , to evaluate the query result to a bool variable, then program could judge the bool variable like :

    if(bExist)
      show("yes, I find it");
    else
      show("sorry, the record doesn't exist in the database");

    I can't imagine I got the bool variable via exception...

    thanks to all.. 

    Filed under: , , , , ,
  • Re: how to know the record I want to query doesn't exist via LINQ?

    08-13-2007, 11:51 AM
    Answer
    • Loading...
    • ndinakar
    • Joined on 05-05-2003, 12:57 PM
    • Orange County, CA
    • Posts 6,835
    • Moderator
      TrustedFriends-MVPs

    In database world you can verify it as follows:

    IF EXISTS ( SELECT * FROM SomeTable WHERE somecolumn = Somecondition)

       BEGIN

         -- do stuff

       END

     

    ***********************
    Dinakar Nethi
    Life is short. Enjoy it.
    ***********************
Page 1 of 1 (2 items)
Microsoft Communities
Page view counter