locked
Error SingleOrDefault not helping RRS feed

  • Question

  • User-1933134441 posted

    my project in raised error "Sequence contains more than one element"

    Project rd = this.db.ProjectSet.SingleOrDefault(x => x.pid == Id);

    i think avoid exception

    Monday, June 24, 2019 1:11 PM

Answers

  • User1520731567 posted

    Hi DevTeams,

    The problem is that you are using SingleOrDefault.

    SingleOrDefault returns a SINGLE element or null if no element is found.

    I believe you are looking for FirstOrDefault which will succeed no matter how many elements are in the collection.

    FirstOrDefault returns the FIRST element it finds or null if no element is found. so if there are 2 elements that match your predicate the second one is ignored.

    Or 

    you may need to use an OrderBy in order to make sure the element you want is indeed first,for example:

    Project rd = this.db.ProjectSet.OrderByDescending(x => x.pid).FirstOrDefault(x => x.pid == Id);
    

    Best Regards.

    Yuki Tao

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, June 25, 2019 2:16 AM
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, June 25, 2019 6:07 AM

All replies

  • User753101303 posted

    Hi,

    The point of using SingleOrDefault is precisely to ensure you get 0 or 1 row or throw otherwise with this message. Id is not a primary key ? Double check your db but it seems that for now you have at least two projects with the same Id which I guess shouldn't happen ?

    Monday, June 24, 2019 1:35 PM
  • User1120430333 posted

    my project in raised error "Sequence contains more than one element"

    Project rd = this.db.ProjectSet.SingleOrDefault(x => x.pid == Id);

    i think avoid exception

    If more than one object can be retured from the query based on the criteria, then FirstOrDefault() should be used, because SingleOrdefault() means only a single object should be found or no bject is found based on the criteria, and if more than one object is found that matches the criteria, an exception is thrown.

    Monday, June 24, 2019 5:49 PM
  • User1520731567 posted

    Hi DevTeams,

    The problem is that you are using SingleOrDefault.

    SingleOrDefault returns a SINGLE element or null if no element is found.

    I believe you are looking for FirstOrDefault which will succeed no matter how many elements are in the collection.

    FirstOrDefault returns the FIRST element it finds or null if no element is found. so if there are 2 elements that match your predicate the second one is ignored.

    Or 

    you may need to use an OrderBy in order to make sure the element you want is indeed first,for example:

    Project rd = this.db.ProjectSet.OrderByDescending(x => x.pid).FirstOrDefault(x => x.pid == Id);
    

    Best Regards.

    Yuki Tao

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, June 25, 2019 2:16 AM
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, June 25, 2019 6:07 AM
  • User-1933134441 posted

    thanks Yuki Tao and addweb solution
    Tuesday, June 25, 2019 12:03 PM