使用联接的linq查询选择属性列表

问题描述 投票:0回答:1

我正在尝试在带有联接的linq中获取列表,并获取特定用户的所有事件。

这是我到目前为止的查询:

  var runnerObject = from r in _context.Runners
                               join re in _context.RunnerEvents
                               on r.RunnerId equals re.RunnerId
                               join e in _context.Events
                               on re.EventId equals e.EventId
                               where r.RunnerId == runnerId
                               select new RunnerVM
                               {
                                   RunnerId = r.RunnerId,
                                   FirstName = r.FirstName,
                                   LastName = r.LastName,
                                   UserId = r.UserId,
                                   Events = //get all events in Events table for the runnerId
                               };

事件应该是该跑步者的事件表中所有条目,基于其在RunnerEvents表中加入的ID。我该怎么办?

谢谢

entity-framework linq linq-to-sql linq-to-entities
1个回答
0
投票

这样的事情?

var runnerObject = from r in _context.Runners
                           join re in _context.RunnerEvents
                           on r.RunnerId equals re.RunnerId
                           join e in _context.Events
                           on re.EventId equals e.EventId
                           where r.RunnerId == runnerId
                           select new RunnerVM
                           {
                               RunnerId = r.RunnerId,
                               FirstName = r.FirstName,
                               LastName = r.LastName,
                               UserId = r.UserId,
                               Events = r.Events.Select(e => new Event { }).ToList()
                           };
© www.soinside.com 2019 - 2024. All rights reserved.