c# - LINQ can't convert to 'int?' -
i trying sort multiple criteria whereas have return id of expensive car , if there multiple cars same value return newest car based on manufacture date.
public int? exercise7demo(list<car> cars) { if (cars == null) { return null; } else { list<car> sorted = cars .orderbydescending( x => x.suggestedretailprice ) .thenby( x => x.manufactureddate ) .tolist(); return sorted.id; } } it getting stuck because can't wrap head around why won't return "sorted". says "can't explicitly convert type system.collections.generic.list 'int?'
what missing? please note can't change public type make work, have thought of part of online demo course doing setup.
edit: should clarify id original defined , part of public int? statement (sorry terminology might off here). reason, if try recall .id in return statement, says not defined. because of list sorted = cars statement?
thanks!
you can do:
return cars.orderbydescending(x => x.suggestedretailprice).thenby(x => x.manufactureddate).firstordefault().id; you not need convert list. more, may impact performance (from msdn):
the tolist(ienumerable) method forces immediate query evaluation , returns list contains query results.
so, suggested way solves problem , keep evaluation lazy.
Comments
Post a Comment