在linq连接谓词中使用“和”

时间:2014-01-13 05:54:48

标签: c# linq c#-4.0 linq-to-sql linq-to-entities

我想在linq中的连接谓词中使用两个字段。如何添加其他字段?

    var Result = from Period in context.PeriodCosts
                 join Name in context.Name on Period.ID equals Name.PeriodID 
                /*"Problem is here ==> */and   
                 Period.CCID equals Name.CCID"
                 select Name;

2 个答案:

答案 0 :(得分:5)

使用匿名类型:

var Result = from Period in context.PeriodCosts
             join Name in context.Name
                 on new { ID = Period.ID, CCID = Period.CCID }
                 equals new { ID = Name.PeriodID, CCID = Name.CCID }
             Period.CCID equals Name.CCID"
             select Name;

答案 1 :(得分:1)

尝试以下

 var Result = from Period in context.PeriodCosts
             join Name in context.Name on 
             new{Period.ID, Period.CCID} equals new { Name.PeriodID, Name.CCID}               
             select Name;