in Linq: How to return a typed object with an overriden values
I have the following linq query which returns a Report Object; yet I need to explicitly set the value of one of its proprties from another column.
Dim results As IQueryable(Of Report)
results = From R In db.Reports _
Join RS In db.ReportStatus On R.ReportID Equals RS.ReportID
Where RS.ActiveIndicator And _
RS.StatusID <> clsCnst.Enum_ReportStatus.COMPLETE
Select R
I want to override R.LatestReportStatusDate = RS.StatusDate. I tried explciitly setting the properties of a new Report object as follows:
Dim results As IQueryable(Of Report)
results = From R In db.Reports _
Join RS In db.ReportStatus On R.ReportID Equals RS.ReportID
Where RS.ActiveIndicator And _
RS.StatusID <> clsCnst.Enum_ReportStatus.COMPLETE
Select New Report With {.ReportID = R.ReportID, _
.ReportTypeID = R.ReportTypeID, _
.PatientID = R.PatientID, _
.Locked = R.Locked, _
.LockedDate = R.LockedDate, _
.LatestReportStatusDate = RS.StatusDate}
However this will give me an error Unable to cast object of type 'System.Data.Linq.DataQuery1[VB$AnonymousType_6
14..... to type 'System开发者_开发问答.Linq.IQueryable`1[reportcirculation.Report]
How can I solve this?
Ok found the solution if someone is interested
Dim make As Func(Of Date, Report, Report) = Function(SD As Date, r As Report)
r.LatestReportStatusDate = SD
Return r
End Function
results = From R In db.Reports _
Join RS In db.ReportStatus On R.ReportID Equals RS.ReportID
Where RS.ActiveIndicator And _
RS.StatusID <> clsCnst.Enum_ReportStatus.COMPLETE
Select make(RS.StatusDate, R)
精彩评论