Sequence contains more than one element

后端 未结 5 2079
醉酒成梦
醉酒成梦 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条回答
  •  孤城傲影
    2020-12-03 02:53

    As @Mehmet is pointing out, if your result is returning more then 1 elerment then you need to look into you data as i suspect that its not by design that you have customers sharing a customernumber.

    But to the point i wanted to give you a quick overview.

    //success on 0 or 1 in the list, returns dafault() of whats in the list if 0
    list.SingleOrDefault();
    //success on 1 and only 1 in the list
    list.Single();
    
    //success on 0-n, returns first element in the list or default() if 0 
    list.FirstOrDefault();
    //success 1-n, returns the first element in the list
    list.First();
    
    //success on 0-n, returns first element in the list or default() if 0 
    list.LastOrDefault();
    //success 1-n, returns the last element in the list
    list.Last();
    

    for more Linq expressions have a look at System.Linq.Expressions

提交回复
热议问题