LINQ一对多,三级深层问题

时间:2010-01-29 21:19:33

标签: c# .net linq linq-to-sql one-to-many

我想在下面的LINQ语句中弄清楚我做错了什么。它不喜欢第三个SELECT。当我输入查询时,它会在Intelisense中找到tblAddresse.tblAdminCounty,但是当我输入SELECT之后它就会发现它。

是否与tblAddresstblAdminCounty有关?我认为它在tblAddress下的Intellisense中显示的事实会使这一陈述不言而喻,但显然不是。

如果我只是在单独的函数中查询CountyName,它将看起来像这样 - >

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

这是基于此网站的更大的3层方法 - > 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
                                   }

                      }
            };

1 个答案:

答案 0 :(得分:2)

您尝试查询它,就好像一个地址有多个县一样。它被称为tblAdminCounty而不是tblAdminCounties的事实是否表明它只是一个项目?

尝试更改此内容:

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

只是:

County = tblAddresse.tblAdminCounty