.net - Return entity that has highest/lowest value in the related entity -
how highest or lowest value related entity? trying use linq/lambda navigation property run nullreferenceexception.
dim e employee = me.employees .orderby(function(x) x.car.car_age.getvalueordefault(1000)) .first() if e isnot nothing return e.car.car_age with data, case example
employee
id name car_id 1 john 1 2 mark 2 3 steve 3 car
id car_age 1 15 2 [null] 3 8 4 19 5 3 employee , car 1-to-1 relationship.
when looking highest value, need employee "john" query. when looking lowest value, need "steve". car_age can null in database.
berfore calling above, entities retrieved database using:
dim query = e in _context.employees.include("car") select e
the stack trace suggests have employees without cars. data have 1 shown in post?
what happens if try instead:
dim e employee = me.employees .where(function(x) x.car isnot nothing) .orderby(function(x) x.car.car_age.getvalueordefault(1000)) .firstordefault() does app still crashes? if not there's problem: employee don't have car.
Comments
Post a Comment