Linq. 3 Joins or Lambda expressions

  • Thread starter Thread starter shapper
  • Start date Start date
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
                                 selectnew 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?

If you're trying to get a parent-child hierarchy, you probably want a
group join. However, this will only give you one level; you will still
have to do a query nesting to get 3 levels, which is what you're doing
here.

If by lambda expressions here you mean the raw syntax with =>, then,
yes, you can write everything with it that can be written with LINQ
syntactic sugar. Just look in MSDN - it describes, for every C# LINQ
keyword, what the translation is.
 
Back
Top