Linq中向实体左联接的Sql查询

问题描述 投票:0回答:1

我在SQL查询以下

Select LC.*,LP.[LandingPageName] from [LandingPageCompanies] LC
 Left join [LandingPageContent] LP on LP.SubCategoryID=LC.SubCategoryID
 where  LC.[CategoryID]=17

而且我需要将其转换为LINQ to entity

我已经尝试过以下查询,但它的工作方式为Inner join

 var data = (from t1 in dbSavingContainer.LandingPageCompanies
                        join t2 in dbSavingContainer.LandingPageContents on t1.SubCategoryID equals t2.SubCategoryID
                        where t1.CategoryID == CategoryID
                        select new
                        {
                            CategoryID = t1.CategoryID,
                            CompanyID = t1.CompanyID,
                            CompanyLink = t1.CompanyLink,
                            CompanyLogo = t1.CompanyLogo,
                            CompanyName = t1.CompanyName,
                            SubCategoryID = t1.SubCategoryID,
                            LandingPageName = t2.LandingPageName
                        }).ToList();

我缺少的地方。 ?

sql-server c#-4.0 entity-framework-4 linq-to-entities
1个回答
1
投票

谢谢..我刚刚使用下面的查询解决了它;)

 var data = (from t1 in dbSavingContainer.LandingPageCompanies
             join t2 in dbSavingContainer.LandingPageContents on t1.SubCategoryID equals t2.SubCategoryID
             into x from y in x.DefaultIfEmpty()
                   where t1.CategoryID == CategoryID
                     select new
                      {
                        CategoryID = t1.CategoryID,
                        CompanyID = t1.CompanyID,
                        CompanyLink = t1.CompanyLink,
                        CompanyLogo = t1.CompanyLogo,
                        CompanyName = t1.CompanyName,
                        SubCategoryID = t1.SubCategoryID,
                        LandingPageName = y.LandingPageName
                    }).ToList();
© www.soinside.com 2019 - 2024. All rights reserved.