开发者

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_614..... 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)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜