我正在加入两个表Race
和Odds
(加入RaceID
),并希望为每个Race创建一个具有嵌套赔率的对象。通过LINQ,我可以将查询结果分组到对象中,但不能为每个Odds list
填充嵌套 Race
。有什么想法吗?
理想情况下,我应该
obj[0] with RaceID = 1, Odds{1,2,3}
obj[1] with RaceID = 2, Odds{4,5,6,7}
等
Public Class Race
Private RaceID As Integer
Private Name As String
'other properties here
Private Odds As New List(Of Odds)
End Class
Public Class Odds
Private OddsID As Integer
Private Odds As System.Nullable(Of Decimal)
Private RaceID As Integer
'other properties here
End Class
Dim objRace As New List(Of Race)
Using context As IDataContext = DataContext.Instance()
Dim repository = context.GetRepository(Of Race)()
objRace = repository.Find("SELECT Odds.OddsID, Odds.Odds, Odds.Name, Odds.Type, Odds.DateUTC, Odds.WebsiteName, Odds.RaceID AS RaceID, Race.RaceID AS RaceID,
Race.Name AS RaceName, Race.RaceDate, Race.Location, Race.URL, Race.PortalID AS PortalID, Race.RaceTime
FROM Odds RIGHT OUTER JOIN
Race ON Odds.RaceID = Race.RaceID
WHERE (Race.PortalID = @0)", PortalId)
End Using
LINQ获取对象
Dim result = objRace.GroupBy(Function(records) records.RaceID).[Select](Function(r) New Race() With {
.RaceID = r.Key
.Odds = ' <------ **somehow populate this nested list**
}).ToList()
答案 0 :(得分:-1)
r
应该是IGrouping
的实现。其中包含您Key
使用的RaceID
,其本身为IEnumerable
。
因此,您可以通过调用Odds
或r.ToList()
来存储r.ToArray
。你怎么想存储它们。
我刚刚注意到您已经在构造函数中为Odds
创建了列表实例。您可能想要更改此设置,因此您可以直接使用With
初始化来收集集合。