I've the next code which adjusts linq organizations to my Domain objects:

return from g in DBContext.Gigs

               choose new DO.Gig

               Title = g.Title,

                   Description  = g.Description,

                   StartDate    = g.Date,

                   EndDate      = g.EndDate,

                   IsDeleted    = g.IsDeleted,

                   Produced      = g.Produced,

                   TicketPrice  = g.TicketPrice

               

This works very nicely.

However would now like to populate a website object Venue object and combine it with the gig within the same statement. Heres my attempt....

return from g in DBContext.Gigs

               join venue in DBContext.Venues on g.VenueID equals venue.ID

               choose new DO.Gig

               Title = g.Title,

                   Description  = g.Description,

                   StartDate    = g.Date,

                   EndDate      = g.EndDate,

                   IsDeleted    = g.IsDeleted,

                   Produced      = g.Produced,

                   TicketPrice  = g.TicketPrice,

                   Venue        =     from v in DBContext.Venues

                                        where v.ID == g.VenueID

                                        choose new DO.Venue

                                        Title         = v.Title,

                                            Address      = v.Address,

                                            Telephone    = v.Telephone,

                                            URL          = v.Website

                                        

               

However this does not compile!!!

Can you really adapt children objects while using "choose new" approach?

What am I doing this super wrong?

Your inner LINQ query returns several objects, not merely one. You need to wrap it having a call like:

Venue = (from v in DBContext.Venues

         where v.ID == g.VenueID

         choose new DO.Venue

         Title         = v.Title,

             Address      = v.Address,

             Telephone    = v.Telephone,

             URL          = v.Website

         ).SingleOrDefault()

The selection of Single() versus. SingleOrDefault() versus. First() versus. FirstOrDefault() is dependent on which type of query it's, but I am speculating you would like among the first two. (The "OrDefault" variants return null when the query doesn't have data others throw.)

I additionally accept Mike that the join is much more consistent with that which you wanted, if there is a singular relationship involved.

The reason for carrying out a join along with a sub choose? You can easily make use of the outcomes of your participate in the roll-out of a brand new Venue. Remember that if there's not really a face to face relationship between gigs and venues you can encounter trouble.

Do this:

return from g in DBContext.Gigs

    join venue in DBContext.Venues on g.VenueID equals venue.ID

    choose new DO.Gig Title = g.Title, Description = g.Description,

        StartDate = g.Date, EndDate = g.EndDate, IsDeleted = g.IsDeleted,

        Produced = g.Produced, TicketPrice = g.TicketPrice,

        Venue = new DO.Venue Title = venue.Title,

            Address = venue.Address, Telephone = v.Telephone,

            URL = v.Website