views:

45

answers:

1

I am trying to figure out what I am doing wrong in the below LINQ statement. It doesn't like the third SELECT. It finds tblAddresse.tblAdminCounty in Intelisense when I am typing the query but when I type the SELECT after it it freaks.

Does it have to do with how tblAddress and tblAdminCounty are related? I would have thought that the fact it shows in Intellisense under tblAddress would make that statement self-evident but obviously not.

If I was to query just the CountyName in a seperate function it would look like this -->

var countyName = from adminCounty in context.tblAdminCounties
                 where adminCounty.CountyID == countyID
                 select adminCounty.CountyName;

And this is the larger 3-tiered approach based on this site --> HERE

var query = from tblBusinesse in context.tblBusinesses
            where tblBusinesse.BusinessID == businessID
            select new
            {
                tblBusinesse.BusinessName,
                tblBusinesse.ContactName,
                tblBusinesse.EmailAddress,
                Address = from tblAddresse in tblBusinesse.tblAddresses 
                      select new 
                      { 
                          tblAddresse.AddressLine1, 
                          tblAddresse.AddressLine2, 
                          tblAddresse.AddressLine3, 
                          tblAddresse.CityName, 
                          County = from adminCounty in tblAddresse.tblAdminCounty
                                   select new
                                   {
                                       adminCounty.CountyName
                                   }

                      }
            };
+2  A: 

You're trying to query it as if a single address has multiple counties. Doesn't the fact that it's called tblAdminCounty rather than tblAdminCounties suggest that it's just a single item?

Try changing this:

County = from adminCounty in tblAddresse.tblAdminCounty
         select new
         {
             adminCounty.CountyName
         }

to just:

County = tblAddresse.tblAdminCounty
Jon Skeet
That did it, thanks! I guess I wasn't fully understanding what the sub-query meant.
Refracted Paladin