S
shapper
Hello,
I have three tables that I need to join: Polls, Options and Votes.
For a given Poll Id I need to get the poll details, its Options and
Votes.
I have the following:
Poll poll = (from p in context.aspnet_Polls
where p.PollId == GivenPollId
select new Poll {
Id = p.PollId,
Active = p.Active,
Created = p.Created,
Options = (from o in p.aspnet_Options
orderby o.Position
select new Option {
Id = o.OptionId,
Answer = o.Answer,
Position = o.Position,
Votes = (from v in o.aspnet_Votes
select new Vote {
Id = v.VoteId,
Created = v.Created
}).ToList()
}).ToList()
}).SingleOrDefault();
How can I change this by using joins? I have the following:
Poll poll2 = (from p in context.aspnet_Polls
join o in context.aspnet_Options on p.PollId equals
o.PollId
join v in context.aspnet_Votes on o.OptionId equals
v.OptionId
where p.PollId == id
select new Poll {
Id = p.PollId,
Active = p.Active,
Created = p.Created,
Options = ????
My problem is with creating the Lists ...
And can I do this using lambda expressions?
I believe the code becomes much shorter ... and I have all the
relations set.
But should I do it?
Thanks,
Miguel
I have three tables that I need to join: Polls, Options and Votes.
For a given Poll Id I need to get the poll details, its Options and
Votes.
I have the following:
Poll poll = (from p in context.aspnet_Polls
where p.PollId == GivenPollId
select new Poll {
Id = p.PollId,
Active = p.Active,
Created = p.Created,
Options = (from o in p.aspnet_Options
orderby o.Position
select new Option {
Id = o.OptionId,
Answer = o.Answer,
Position = o.Position,
Votes = (from v in o.aspnet_Votes
select new Vote {
Id = v.VoteId,
Created = v.Created
}).ToList()
}).ToList()
}).SingleOrDefault();
How can I change this by using joins? I have the following:
Poll poll2 = (from p in context.aspnet_Polls
join o in context.aspnet_Options on p.PollId equals
o.PollId
join v in context.aspnet_Votes on o.OptionId equals
v.OptionId
where p.PollId == id
select new Poll {
Id = p.PollId,
Active = p.Active,
Created = p.Created,
Options = ????
My problem is with creating the Lists ...
And can I do this using lambda expressions?
I believe the code becomes much shorter ... and I have all the
relations set.
But should I do it?
Thanks,
Miguel