I was getting below error while querying “AccountSet” using LINQ in my plug-in
System.InvalidOperationException: Sequence contains no elements
Reason:-
- Exception raised when there were no records that matches to the criteria specified in my LINQ query
- I had my query statement as below
var account = orgContext.accountSet.Where(acct => (acct.name == “Rajeev”)).First();
- There were no ‘Accounts’ with name=Rajeev and using of First() resulted “Sequence contains no elements” exception
Fix:-
- Replace the method First() with FirstOrDefault().
- The method FirstOrDefault() returns a null value, if there are no records that matches to the filtering criteria
- Now my LINQ expression looks as below
var account = orgContext.accountSet.Where(acct => (acct.name == “Rajeev”)). FirstOrDefault();
🙂


![[Step by Step] Configure and consume 'Environment Variables' of type 'Secret' using 'Azure Key vault'](https://rajeevpentyala.com/wp-content/uploads/2023/05/image.png)
Leave a comment