Sequence contains more than one element

后端 未结 5 2057
醉酒成梦
醉酒成梦 2020-12-03 02:11

I\'m having some issues with grabbing a list of type \"RhsTruck\" through Linq and getting them to display.

RhsTruck just has properites Make, Model, Serial etc...

5条回答
  •  -上瘾入骨i
    2020-12-03 02:48

    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);
    

提交回复
热议问题