locked
returning a single row from a table RRS feed

  • Question

  • User172691333 posted

    Hi,

    I found this two way of returning a single row from my a table, is there anybody who can explain their difference to me?

    A)            Test selectedTest = Context.Tests.Single(t => t.ID == ID);

    B(             Test selectedTest = (from T in Context.Tests
                                         where T.ID == ID
                                         select T).SingleOrDefault();

    Wednesday, January 20, 2016 12:45 AM

Answers

  • User-271186128 posted

    Hi Kia,

    kia997997

    The subject is: "Object reference not set to an instance of an object / Sequence contains no elements ??"

    As for this issue, I suppose the query result is null, please check your database and make sure it contains the specify records.

    You could add a IF Else statement to check the result, like  this:

                    Test selectedTest = (from T in Context.Tests
                                        where T.ID == ID
                                        select T).SingleOrDefault();
    if(selectedTest != null)
    {
                    selectedTest.ID = ID;
                    selectedTest.Stat = "Deleted";
    Context.SubmitChanges();
    }

    Best regards,
    Dillion

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, January 20, 2016 3:00 AM

All replies

  • User533502567 posted

    For following code: You will get default item if no records exists in the table

    B(             Test selectedTest = (from T in Context.Tests
                                         where T.ID == ID
                                         select T).SingleOrDefault();

    For following code: You will get exception if no records exists in the table.

            Test selectedTest = Context.Tests.Single(t => t.ID == ID);

    When the sequence contains many elements(more than one) -- You will get an exception in both cases.

    If you want one record no matter what the sequence contains, use FirstOrDefault.

    Wednesday, January 20, 2016 1:09 AM
  • User172691333 posted

    Thanks a lot budugu for the clarification.

     I actually tried both in a context and I got different errors for each.

    I posted another another question a minut ago explaining the context, It will be great if you can help me with that too.

    The subject is: "Object reference not set to an instance of an object / Sequence contains no elements ??"

    Thanks again,

    Kia

    Wednesday, January 20, 2016 1:19 AM
  • User-271186128 posted

    Hi Kia,

    kia997997

    The subject is: "Object reference not set to an instance of an object / Sequence contains no elements ??"

    As for this issue, I suppose the query result is null, please check your database and make sure it contains the specify records.

    You could add a IF Else statement to check the result, like  this:

                    Test selectedTest = (from T in Context.Tests
                                        where T.ID == ID
                                        select T).SingleOrDefault();
    if(selectedTest != null)
    {
                    selectedTest.ID = ID;
                    selectedTest.Stat = "Deleted";
    Context.SubmitChanges();
    }

    Best regards,
    Dillion

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, January 20, 2016 3:00 AM