how to know the record I queried doesn't exist in the database via LINQ?

Last post 09-16-2007 10:00 PM by Moosdau. 6 replies.

Sort Posts:

  • how to know the record I queried doesn't exist in the database via LINQ?

    08-13-2007, 10:12 PM
    • Loading...
    • Moosdau
    • Joined on 05-10-2007, 11:49 PM
    • China
    • Posts 48

    Hi all:

    for example:
    var res=db.tables.single(p=>p.columnName==3);

    if the record exist, it works well, but if it doesn't exist, it will throw an exception, I don't think exception is a reasonable way to know there isn't a record exist in the database, so , how to handle the case if record doesn't exist?

    thanks in advance..

    Filed under: ,
  • Re: how to know the record I queried doesn't exist in the database via LINQ?

    08-14-2007, 9:24 AM
    • Loading...
    • SattaMassaGana
    • Joined on 11-01-2005, 11:03 AM
    • Birmingham, England
    • Posts 36

    Not sure what your datasource is but SQl provides the EXISTS() function for just such a situation.  If you use a stored procedure which contains this function then you'll not have to rely on exception handling to deal with this in your code pages.

     Regards

     SattaMassaGana

  • Re: how to know the record I queried doesn't exist in the database via LINQ?

    08-14-2007, 10:12 AM

    Get an sqldatareader.

    and then use

    imagine a reader object and command object

    reader = cmd.ExecuteDataReader

    if (reader.Read())

    //Do stuff

     

    it will only work if there is data.

    Please Mark Post that helped you as answer, also include a summary of what solved the problem as it helps others in similar situations
  • Re: how to know the record I queried doesn't exist in the database via LINQ?

    08-19-2007, 8:30 PM
    • Loading...
    • Moosdau
    • Joined on 05-10-2007, 11:49 PM
    • China
    • Posts 48

    Thanks for replying~

     Yes, I know, and also I could use :
    var res=from ....where ....select .
    via from & where & select , it won't throw exception if the record doesn't exist.

    but since microsoft provide lambda, and lambda is simpler, I want to use lambda if possible,
    now .... I think this is unbelivable , isn't it useless?

    naturehermit:

    Get an sqldatareader.

    and then use

    imagine a reader object and command object

    reader = cmd.ExecuteDataReader

    if (reader.Read())

    //Do stuff

     

    it will only work if there is data.

  • Re: how to know the record I queried doesn't exist in the database via LINQ?

    08-27-2007, 10:52 PM
    • Loading...
    • jwooley
    • Joined on 05-02-2003, 10:07 AM
    • Atlanta, GA
    • Posts 15

    If you only need to know if it exists, how about the following:

    var res = db.tables.Where(p => p.ColumnName==3).Count();

    if (res>0) ...

    This results in a Select Count(*) from Table Where ColumnName=@p0 query.

  • Re: how to know the record I queried doesn't exist in the database via LINQ?

    09-13-2007, 3:51 PM
    Answer
    • Loading...
    • joerattz
    • Joined on 03-10-2003, 1:02 PM
    • Atlanta, GA
    • Posts 155

    Instead of using the Single operator, use SingleOrDefault.  Or, you could use FirstOrDefault (as opposed to First).

    Pro LINQ: Language Integrated Query in C# 2008
    http://www.linqdev.com
    http://www.netsplore.com
  • Re: how to know the record I queried doesn't exist in the database via LINQ?

    09-16-2007, 10:00 PM
    • Loading...
    • Moosdau
    • Joined on 05-10-2007, 11:49 PM
    • China
    • Posts 48

    Hello Joerattz:

      Thanks very much! Your answer is exactly.

     And also thanks others who have answered my question----thank you all! 

    joerattz:

    Instead of using the Single operator, use SingleOrDefault.  Or, you could use FirstOrDefault (as opposed to First).

Page 1 of 1 (7 items)
Microsoft Communities
Page view counter