如何使用lambda表达式连接2个表

时间:2013-12-23 17:01:45

标签: sql linq entity-framework c#-4.0 lambda

任何人都可以告诉我如何使用lambda表达式编写这个SQL查询。

我正在尝试加入CoachingLang.ExpandedForm& CoachingLang.ExpandedCategory到CoachingDef.FormCategory。

SELECT Ef.Translation +' - '+ Ec.Translation, Fc.FormCategoryID, Ef.FormId,Ef.Translation, Ec.CategoryId, Ec.Translation
FROM CoachingDef.FormCategory Fc
INNER JOIN CoachingLang.ExpandedForm Ef ON Fc.FormID = Ef.FormId 
AND Ef.DictionaryId = -2147483645 AND Ef.PropertyId = -2147483647
INNER JOIN CoachingLang.ExpandedCategory Ec ON Fc.CategoryID = Ec.CategoryId
AND Ec.DictionaryId = -2147483645 AND Ec.PropertyId = -2147483647

2 个答案:

答案 0 :(得分:3)

from Fc in CoachingDef.FormCategory
join Ef in CoachingLang.ExpandedForm on Fc.FormID equals Ef.FormId 
join Ec in CoachingLang.ExpandedCategory on Fc.CategoryID equals Ec.CategoryId
where Ef.DictionaryId = -2147483645 && Ef.PropertyId = -2147483647
&& Ec.DictionaryId = -2147483645 && Ec.PropertyId = -2147483647

select new {Translation = Ef.Translation +' - '+ Ec.Translation, 
    Fc.FormCategoryID, Ef.FormId,Ef.Translation, 
    Ec.CategoryId, Ec.Translation}

答案 1 :(得分:2)

联接在Lambda形式中很混乱,但这里是:

 CoachingDef.FormCategory
            .Join(CoachingLang.ExpandedForm,
                  Fc => new {Fc.FormID, 
                             DictionaryId = -2147483645, 
                             PropertyId = -2147483647}  // primary key
                  Ef => new {Ef.FormId, 
                             Ef.DictionaryId,
                             Ef.PropertyId}   // foreign key
                  (Fc,Ef) => new {Fc, Ef})
            .Join(CoachingLang.ExpandedCategory,   // the "left" side is now the merged Fc/Ef type
                  FcEf => new {FeEf.Fc.CategoryID, 
                               DictionaryId = -2147483645, 
                               PropertyId = -2147483647}  // primary key
                  Ec => new {Ec.CategoryId, 
                             Ec.DictionaryId,
                             Ec.PropertyId}   // foreign key
                  (FcEf,Ec) => new {FcEf.Fc, FcEf.Ef, Ec})
             .Select(FcEfEc => new {Translation = FcEfEc.Ef.Translation +' - '+ FcEfEc.Ec.Translation, 
                                    FcEfEc.Fc.FormCategoryID, 
                                    FcEfEc.Ef.FormId, 
                                    FcEfEc.Ef.Translation, 
                                    Ec.CategoryId, 
                                    Ec.Translation
                                   })

查询语法更清晰:

from Fc in CoachingDef.FormCategory
join Ef in CoachingLang.ExpandedForm on 
    new {Fc.FormID, 
         DictionaryId = -2147483645, 
         PropertyId = -2147483647} 
    equals new { Ef.FormId, 
         Ef.DictionaryId, 
         Ef.PropertyId}  
join Ec in CoachingLang.ExpandedCategory on 
    new {Fc.CategoryID, 
         DictionaryId = -2147483645, 
         PropertyId = -2147483647}
    equals new Ec.CategoryId, 
         Ec.DictionaryId, 
         Ec.PropertyId}
select new {Translation = Ef.Translation +' - '+ Ec.Translation, 
            Fc.FormCategoryID, 
            Ef.FormId,
            Ef.Translation, 
            Ec.CategoryId, 
            Ec.Translation}