views:

399

answers:

2

Hi,

I have the following code which adapts linq entities to my Domain objects:

return from g in DBContext.Gigs
               select new DO.Gig
               {
                   ID = g.ID,
                   Name = g.Name,
                   Description  = g.Description,
                   StartDate    = g.Date,
                   EndDate      = g.EndDate,
                   IsDeleted    = g.IsDeleted,
                   Created      = g.Created,
                   TicketPrice  = g.TicketPrice

               };

This works very nicely.

However I now want to populate a domain object Venue object and add it to the gig in the same statement. Heres my attempt....

return from g in DBContext.Gigs
               join venue in DBContext.Venues on g.VenueID equals venue.ID
               select new DO.Gig
               {
                   ID = g.ID,
                   Name = g.Name,
                   Description  = g.Description,
                   StartDate    = g.Date,
                   EndDate      = g.EndDate,
                   IsDeleted    = g.IsDeleted,
                   Created      = g.Created,
                   TicketPrice  = g.TicketPrice,
                   Venue        =     from v in DBContext.Venues
                                        where v.ID == g.VenueID
                                        select new DO.Venue
                                        {
                                            ID           = v.ID,
                                            Name         = v.Name,
                                            Address      = v.Address,
                                            Telephone    = v.Telephone,
                                            URL          = v.Website 
                                        }

               };

However this doesnt compile!!!

Is it possible to adapt children objects using the "select new" approach?

What am I doing so very very wrong?

+1  A: 

Why are you doing a join and a sub select? You can just use the results of your join in the creation of a new Venue. Be aware that if there is not a one to one relationship between gigs and venues you could run into trouble.

Try this:

return from g in DBContext.Gigs 
    join venue in DBContext.Venues on g.VenueID equals venue.ID 
    select new DO.Gig { ID = g.ID, Name = g.Name, Description = g.Description,
        StartDate = g.Date, EndDate = g.EndDate, IsDeleted = g.IsDeleted, 
        Created = g.Created, TicketPrice = g.TicketPrice, 
        Venue = new DO.Venue { ID = venue.ID, Name = venue.Name, 
            Address = venue.Address, Telephone = v.Telephone, 
            URL = v.Website }
Mike Two
+2  A: 

Your inner LINQ query returns several objects, not just one. You want to wrap it with a call like:

Venue = (from v in DBContext.Venues
         where v.ID == g.VenueID
         select new DO.Venue
         {
             ID           = v.ID,
             Name         = v.Name,
             Address      = v.Address,
             Telephone    = v.Telephone,
             URL          = v.Website 
         }).SingleOrDefault()

Your choice of Single() vs. SingleOrDefault() vs. First() vs. FirstOrDefault() depends on what kind of query it is, but I'm guessing you want one of the first two. (The "OrDefault" variants return null if the query has no data; the others throw.)

I also agree with Mike that a join might be more in line with what you wanted, if there's a singular relationship involved.

Brad Wilson