有人可以告诉我最短的查询:
var guestpartyids = db.CeremonyGuestParties.Where(p => p.CeremonyId == id)
.Select(p => p.GuestPartyId);
List<GuestParty> guestparties = new List<GuestParty>();
foreach (var party in guestpartyids)
{
guestparties.Add(db.GuestParties.Single(p => p.Id == party));
}
答案 0 :(得分:2)
这应该这样做。
guestparties.AddRange(
from cgp in db.CeremonyGuestParties
where cgp.CeremonyId == id
join gp in db.GuestParties on cgp.GuestPartyId equals gp.Id
select gp
);
请注意,这将导致一次数据库调用,因为您的代码将导致1 + N次查询。但它不能确保只有一个匹配的ID,如Single()会这样做。这应该在数据库上强制执行,而不是代码。
答案 1 :(得分:0)
怎么样:
List<GuestParty> guestparties = from cgp in db.CeremonyGuestParties.
Where cgp.CeremonyId .id == id)
select cgp.Guestparties.ToList();