开发者

LINQ One to Many, 3 levels Deep Question

开发者 https://www.devze.com 2022-12-18 07:39 出处:网络
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 t

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 seperat开发者_如何学运维e 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
                                   }

                      }
            };


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
0

精彩评论

暂无评论...
验证码 换一张
取 消