Sequence contains more than one element Sequence contains more than one element asp.net asp.net

Sequence contains more than one element


The problem is that you are using SingleOrDefault. This method will only succeed when the collections contains exactly 0 or 1 element. I believe you are looking for FirstOrDefault which will succeed no matter how many elements are in the collection.


SingleOrDefault method throws an Exception if there is more than one element in the sequence.

Apparently, your query in GetCustomer is finding more than one match. So you will either need to refine your query or, most likely, check your data to see why you're getting multiple results for a given customer number.


Use FirstOrDefault insted of SingleOrDefault..

SingleOrDefault returns a SINGLE element or null if no element is found. If 2 elements are found in your Enumerable then it throws the exception you are seeing

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

   public int GetPackage(int id,int emp)           {             int getpackages=Convert.ToInt32(EmployerSubscriptionPackage.GetAllData().Where(x   => x.SubscriptionPackageID ==`enter code here` id && x.EmployerID==emp ).FirstOrDefault().ID);               return getpackages;           } 1. var EmployerId = Convert.ToInt32(Session["EmployerId"]);               var getpackage = GetPackage(employerSubscription.ID, EmployerId);