【发布时间】:2015-02-21 05:47:18
【问题描述】:
我正在使用实体框架。
我有这个案例:
Partial Public Class Myobj
Public Property id As Integer
Public property name as string
Public Overridable Property chld As ICollection(Of chld) = New HashSet(Of chld)
End Class
Partial Public Class Myobj
Public shared cond1 as DateTime
<NotMapped> Public ReadOnly Property vls As integer
Get
Return chld.AsQueryable.Where(Function(t2) t2.date1<cond1).Select(Function(t3) t3.quantity).DefaultIfEmpty.Sum()
End Get
End Property
End Class
Partial Public Class chld
Public Property id As Integer
Public Property date1 as DateTime
Public Property quantity as Integer
Public Property ParentID as integer
Public Overridable Property MyObj1 As MyObj
End Class
现在在我的表单上,我有这个代码:
Dim dt1 as DateTime=CDate("08/08/2014")
Myobj.cond1=dt1
Dim list1 = (From t In context.MyObj Select New With { _
.Parent = t, _
.chl = (From t2 In t.chld.AsQueryable.Where(Function(t3) t3.Date1>=dt1) Select t2)
}).ToList
如您所见,未映射属性vls 计算的是日期“08/08/2014”之前的孩子数量的总和。
在主查询中选择日期在“08/08/2014”之后的孩子
对于 Myobj1 中的每个项目,此查询总是在 vls 属性中生成 0。(!!但根据数据库中的数据,它不是真的!!)。
为什么这个查询会产生这样的结果?
谢谢!
【问题讨论】: