【发布时间】:2014-03-24 20:53:25
【问题描述】:
如何从相关实体中查找最高或最低值? 我试图将 LINQ/lambda 与导航属性一起使用,但遇到了 NullReferenceException。
Dim e As employee = Me.employees
.OrderBy(Function(x) x.car.car_age.GetValueOrDefault(1000))
.First()
If e IsNot Nothing Then Return e.car.car_age
有数据,案例举例
员工
id name car_id
1 John 1
2 Mark 2
3 Steve 3
汽车
id car_age
1 15
2 [Null]
3 8
4 19
5 3
员工和汽车一对一的关系。
在寻找最高价值时,我需要从查询中获取员工“John”。 在寻找最低价值时,我需要“史蒂夫”。 car_age 在数据库中也可以为空。
在调用上述方法之前,使用以下方法从数据库中检索实体:
Dim query = From e In _context.employees.Include("car")
Select e
【问题讨论】:
-
NullReferenceException的StackTrace值是多少? -
在 MyNS.MyModel.company._Lambda$__41(employee x) in C:/Temp... 在 System.Linq.EnumerableSorter
2.ComputeKeys(TElement[] elements, Int32 count) at System.Linq.EnumerableSorter1.Sort(TElement[] 元素,Int32 计数) 在 System.Linq.OrderedEnumerable1.<GetEnumerator>d__0.MoveNext() at System.Linq.Enumerable.First[TSource](IEnumerable1 源) 在 MyNS.MyModel.company.get_OldestCar()
标签: .net linq entity-framework lambda navigation-properties