Linq To SQL中的关联显示为EntitySet<>,为什么?

时间:2010-04-28 17:15:27

标签: c# .net linq linq-to-sql stored-procedures

我在使用Linq等效的遗留存储过程时遇到了很多麻烦。最大的障碍是它似乎不想让我在tblAddress的加入上添加第二个'子句'。我收到Cannot resolve method...错误。 tblBusiness.tblAddress被视为EntitySet<tblAddress>查看当前工作的底部。

有谁可以指出我做错了什么?下面是第一个SPROC我需要转换,第二个,我的LINQ尝试到目前为止;这是完全失败!

由于

SELECT dbo.tblPersonInsuranceCoverage.PersonInsuranceCoverageID, 
    dbo.tblPersonInsuranceCoverage.EffectiveDate, 
    dbo.tblPersonInsuranceCoverage.ExpirationDate, 
    dbo.tblPersonInsuranceCoverage.Priority, 
    dbo.tblAdminInsuranceCompanyType.TypeName AS CoverageCategory, 
    dbo.tblBusiness.BusinessName, 
    dbo.tblAdminInsuranceType.TypeName AS TypeName,
    CASE WHEN dbo.tblAddress.AddressLine1 IS NULL THEN '' ELSE dbo.tblAddress.AddressLine1 END 
    + ' ' + 
    CASE WHEN dbo.tblAddress.CityName IS NULL THEN '' ELSE '<BR>' + dbo.tblAddress.CityName END 
    + ' ' + 
    CASE WHEN dbo.tblAddress.StateID IS NULL THEN '' 
         WHEN dbo.tblAddress.StateID = 'ns' THEN '' 
         ELSE dbo.tblAddress.StateID END AS Address
FROM      
    dbo.tblPersonInsuranceCoverage 
        LEFT OUTER JOIN dbo.tblInsuranceCompany 
            ON dbo.tblPersonInsuranceCoverage.InsuranceCompanyID = dbo.tblInsuranceCompany.InsuranceCompanyID 
                LEFT OUTER JOIN dbo.tblBusiness     
                    ON dbo.tblBusiness.BusinessID = dbo.tblInsuranceCompany.BusinessID 
                        LEFT OUTER JOIN dbo.tblAddress 
                            ON dbo.tblAddress.BusinessID = dbo.tblBusiness.BusinessID and tblAddress.AddressTypeID = 'b' 

        LEFT OUTER JOIN dbo.tblAdminInsuranceCompanyType 
            ON dbo.tblPersonInsuranceCoverage.InsuranceCompanyTypeID = dbo.tblAdminInsuranceCompanyType.InsuranceCompanyTypeID  

        LEFT OUTER JOIN dbo.tblAdminInsuranceType 
            ON dbo.tblPersonInsuranceCoverage.InsuranceTypeID = dbo.tblAdminInsuranceType.InsuranceTypeID   

WHERE tblPersonInsuranceCoverage.PersonID = @PersonID

 var coverage = 
                    from insuranceCoverage in context.tblPersonInsuranceCoverages
                    where insuranceCoverage.PersonID == personID

                select
                    new
                        {
                            insuranceCoverage.PersonInsuranceCoverageID,
                            insuranceCoverage.EffectiveDate,
                            insuranceCoverage.ExpirationDate,
                            insuranceCoverage.Priority,
                            CoverageCategory = insuranceCoverage.tblInsuranceCompany.tblAdminInsuranceCompanyType.TypeName,
                            insuranceCoverage.tblInsuranceCompany.tblBusiness.BusinessName,
                            TypeName = insuranceCoverage.InsuranceTypeID,
                            Address = insuranceCoverage.tblInsuranceCompany.tblBusiness.tblAddresses
                                                                        .Where(a => a.AddressTypeId = 'b')
                                                                        .FirstOrDefault()
                            };

编辑进一步尝试

所以我在dbml中添加了一些关联,以便我可以在下面看到Craigs的建议。它几乎可以工作。现在我在Cannot resolve symbol上获得了a.AddressTypeID。奇怪的是,Intellisense告诉我tblAddressEntitySet<tblAdress>。我错过了一个协会,或者我有一个不正确的协会,还是我只是处于很多层次?

思想?

2 个答案:

答案 0 :(得分:2)

It's usually wrong (and way too much work) to use join in LINQ to SQL。而是使用L2S为您生成的导航/关联属性:

var coverage = 
                from insuranceCoverage in context.tblPersonInsuranceCoverages
                where insuranceCoverage.PersonID == personID
                select new
                    {
                            insuranceCoverage.PersonInsuranceCoverageID,
                            insuranceCoverage.EffectiveDate,
                            insuranceCoverage.ExpirationDate,
                            insuranceCoverage.Priority,
                            CoverageCategory = insuranceCoverage.insuranceCompany.tblAdminInsuranceCompanyType.TypeName,
                            insuranceCoverage.insuranceCompany.tblBusiness.BusinessName,
                            TypeName = insuranceCoverage.InsuranceTypeID,
                            Address = insuranceCoverage.insuranceCompany.Addresses
                                                                        .Where(a => a.AddressTypeID == 'b')
                                                                        .FirstOrDefault()
                        };

答案 1 :(得分:1)

我不知道它是否会完成你需要的一切,但你可以使用匿名类型为equijoins和复合键:

 from x in table1
 join y in table2 on new { x.Id1, x.Id2 } equals new { y.Id1, y.Id2 }
 ...

看看是否有帮助。