Skip to main content
Solved

Sequence contains no element error when select result is null


aaghaei
Captain II
Forum|alt.badge.img+10

Hello gang,

 

I have the below select in one of my methods. When there is data that meets the conditions, it works fine but when there is no data with this conditions I get an error “Sequence contains no element”. How can I prevent system from raising error. I’m checking this select result later in my code whether is null or not.

 

                EPApproval ownerApproval = PXSelect<EPApproval,
                            Where<EPApproval.refNoteID, Equal<Required<APInvoice.noteID>>,
                            And<EPApproval.ownerID, Equal<Required<AccessInfo.contactID>>,
                            And<EPApproval.status, Equal<EPStatuses.pending>>>>,
                            OrderBy<Asc<EPApproval.approvalID>>>
                           .Select(this.Base, this.Base.Document.Current.NoteID, PXAccess.GetContactID().Value, EPStatuses.Pending).First();

 


 

Best answer by Shawn Burt

 

aaghaei,

I have had this issue if the result is empty, you might change to FirstOrDefault() instead. 

FirstOrDefault will return a null if the list is empty. First will return the error you are getting if results are empty.

View original
Did this topic help you find an answer to your question?

8 replies

Naveen Boga
Captain II
Forum|alt.badge.img+19
  • Captain II
  • 3406 replies
  • April 8, 2022

Hi @aaghaei  Please use below query and let me know if you have any issues?

 

   EPApproval ownerApproval = PXSelect<EPApproval,
                            Where<EPApproval.refNoteID, Equal<Required<EPApproval.refNoteID>>,
                            And<EPApproval.ownerID, Equal<Current<AccessInfo.contactID>>,
                            And<EPApproval.status, Equal<Required<EPApproval.status>>>>>,
                            OrderBy<Asc<EPApproval.approvalID>>>
                           .Select(Base, this.Base.Document.Current?.NoteID, EPStatuses.Pending);

 


aaghaei
Captain II
Forum|alt.badge.img+10
  • Author
  • Captain II
  • 1202 replies
  • April 8, 2022

Thanks @Naveen Boga ,

Now I get this error “The multi-part identifier "AccessInfo.ContactID" could not be bound.”


Naveen Boga
Captain II
Forum|alt.badge.img+19
  • Captain II
  • 3406 replies
  • April 8, 2022

Hi @aaghaei Sorry, Please try below query!!

  EPApproval ownerApproval = PXSelect<EPApproval,
                                        Where<EPApproval.refNoteID, Equal<Required<EPApproval.refNoteID>>,
                                        And<EPApproval.ownerID, Equal<Required<EPApproval.ownerID>>,
                                        And<EPApproval.status, Equal<Required<EPApproval.status>>>>>,
                                        OrderBy<Asc<EPApproval.approvalID>>>
                                       .Select(Base, this.Base.Document.Current?.NoteID, Base.Accessinfo.ContactID, EPStatuses.Pending);

 


aaghaei
Captain II
Forum|alt.badge.img+10
  • Author
  • Captain II
  • 1202 replies
  • April 8, 2022

Thanks @Naveen Boga 

 

Now it is back to square 1. “Sequence contains no elemen” please note I only need the top 1 so I have .First() at the end. here is the code I run

 

                EPApproval ownerApproval = PXSelect<EPApproval,
                            Where<EPApproval.refNoteID, Equal<Required<EPApproval.refNoteID>>,
                            And<EPApproval.status, Equal<Required<EPApproval.status>>,
                            And<EPApproval.ownerID, Equal<Required<EPApproval.ownerID>>>>>,
                            OrderBy<Asc<EPApproval.approvalID>>>
                            .Select(Base, Base.Document.Current?.NoteID, EPStatuses.Pending, Base.Accessinfo.ContactID).First();

//also I tried this

                EPApproval ownerApproval = PXSelect<EPApproval,
                            Where<EPApproval.refNoteID, Equal<Required<EPApproval.refNoteID>>,
                            And<EPApproval.status, Equal<Required<EPApproval.status>>,
                            And<EPApproval.ownerID, Equal<Required<EPApproval.ownerID>>>>>,
                            OrderBy<Asc<EPApproval.approvalID>>>
                            .Select(Base, Base.Document.Current?.NoteID, EPStatuses.Pending, Base.Accessinfo?.ContactID).First();

//and tried this
        int CurrentOwnerID = PXAccess.GetContactID().Value;

                EPApproval ownerApproval = PXSelect<EPApproval,
                            Where<EPApproval.refNoteID, Equal<Required<EPApproval.refNoteID>>,
                            And<EPApproval.status, Equal<Required<EPApproval.status>>,
                            And<EPApproval.ownerID, Equal<Required<EPApproval.ownerID>>>>>,
                            OrderBy<Asc<EPApproval.approvalID>>>
                            .Select(Base, Base.Document.Current?.NoteID, EPStatuses.Pending, CurrentOwnerID).First();

 


Naveen Boga
Captain II
Forum|alt.badge.img+19
  • Captain II
  • 3406 replies
  • April 8, 2022

Hi @aaghaei  If you wanted to fetch the TOP 1 record, please use the SELECTWINDOWED syntax below and NO need to FIRST

 

Here is the example:


            EPApproval ownerApproval = PXSelect<EPApproval,
                         Where<EPApproval.refNoteID, Equal<Required<EPApproval.refNoteID>>,
                         And<EPApproval.status, Equal<Required<EPApproval.status>>,
                         And<EPApproval.ownerID, Equal<Required<EPApproval.ownerID>>>>>,
                         OrderBy<Asc<EPApproval.approvalID>>>
                         .SelectWindowed(Base, 0, 1, Base.Document.Current?.NoteID, EPStatuses.Pending, Base.Accessinfo.ContactID);

 


Shawn Burt
Jr Varsity I
Forum|alt.badge.img+1
  • Jr Varsity I
  • 109 replies
  • Answer
  • April 8, 2022

 

aaghaei,

I have had this issue if the result is empty, you might change to FirstOrDefault() instead. 

FirstOrDefault will return a null if the list is empty. First will return the error you are getting if results are empty.


Dmitrii Naumov
Acumatica Moderator
Forum|alt.badge.img+7
  • Acumatica Moderator
  • 632 replies
  • April 8, 2022

You can also just skip the First() completely. 

The engine is smart enough to recognize that if you save the result to variable of type EPApproval and not IEnumerable<EPApproval> that you expect only one result. The engine will automatically add the TOP1 to the SQL query if you save the query result to such variable. 


aaghaei
Captain II
Forum|alt.badge.img+10
  • Author
  • Captain II
  • 1202 replies
  • April 9, 2022

Thank you @Dmitrii Naumov, @Shawn Burt and @Naveen Boga 

 

much appreciated


Reply


Cookie policy

We use cookies to enhance and personalize your experience. If you accept you agree to our full cookie policy. Learn more about our cookies.

 
Cookie settings