开发者

Association in Linq To SQL showing as an EntitySet<>, why?

I am having a lot of trouble coming up with the Linq equivalent of this legacy stored procedure. The biggest hurdle is it doesn't seem to want to let me add a second 'cla开发者_运维百科use' on the join with tblAddress. I am getting a Cannot resolve method... error. that tblBusiness.tblAddress is seen as an EntitySet<tblAddress> See bottom for current effort.

Can anyone point out what I am doing wrong? Below is, first, the SPROC I need to convert and, second, my LINQ attempt so far; which is FULL OF FAIL!

Thanks

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()
                            };

EDIT for further attempt

So I added some associations in the dbml so that I could take Craigs advice below. It almost works. Now I am getting a Cannot resolve symbol on a.AddressTypeID. What is strange is that Intellisense tells me that tblAddress is an EntitySet<tblAdress>. Am I missing an association or do I have an improper one or am I just TOO many levels deep?

Thoughts?


It's usually wrong (and way too much work) to use join in LINQ to SQL. Instead, use the navigation/association properties L2S generates for you:

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()
                        };


I don't know whether it'll do everything you need, but you can use anonymous types for equijoins with a composite key:

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

See if that helps.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜