将2个右外连接用文字转换为LINQ

时间:2017-06-12 21:12:13

标签: c# entity-framework linq

我有以下SQL Server查询,这正是我想要的:

SELECT        
  s.StateAbbr, 
  s.StateName, 
  s.SutaBasis, 
  s.ERRate, 
  s.HighRate, 
  p.PeoCode, 
  p.SutaReportingType, 
  p.SutaCost, 
  dc.ColorValue
FROM DefaultMapColors dc
RIGHT OUTER JOIN PEOCoverage_XRef p ON dc.CategoryName = p.SutaReportingType 
RIGHT OUTER JOIN SutaWageBasis s ON p.StateCode = s.StateAbbr AND p.PeoCode = 'VHR'

然而,我有点时间将其转换为LINQ。这就是我认为它应该是:

var query = (from dc in DefaultMapColors
             join p in PEOCoverage_XRefs on dc.CategoryName equals p.PeoCode into p1 from p in p1.DefaultIfEmpty()
             join s in SutaWageBasis on new { p.StateCode, p.PeoCode } equals new { s.StateAbbr, "VHR" } into p2 from s in p2.DefaultIfEmpty()
             select new
             {
               // snipped for brevity
             }).ToList();

但我得到的错误是“无效的匿名类型成员声明符。必须使用成员赋值,简单名称或成员访问来声明匿名类型成员。我很确定它是文字”VHR“,但它必须在那里是为了得到我需要的结果。

任何人都可以帮我解决这个问题吗?

2 个答案:

答案 0 :(得分:0)

p.PeoCode = 'VHR'不是加入的一部分 - 它只是一个地方。所以,编码就像一个:

var query = (from dc in DefaultMapColors
         join p in PEOCoverage_XRefs on dc.CategoryName equals p.PeoCode into p1 
         from p in p1.DefaultIfEmpty()
         join s in SutaWageBasis on p.StateCode equals s.StateAbbr into p2
         from s in p2.DefaultIfEmpty()
         where p.PeoCode == "VHR"
         select new
         {
           // snipped for brevity
         }).ToList();

(我无法测试,所以我不知道其余的是正确的......)

答案 1 :(得分:0)

好的,我明白了。

var query2 = (from s in SutaWageBasis
              join p in PEOCoverage_XRefs on new { sc = s.StateAbbr, pc = "VHR" } equals new { sc = p.StateCode, pc = p.PeoCode } into p1
              from p in p1.DefaultIfEmpty()
              join dc in DefaultMapColors on p.PeoCode equals dc.CategoryName into p2
              from dc in p2.DefaultIfEmpty()
              select new SutaStateCoverage
              {
                  StateAbr = s.StateAbbr,
                  StateName = s.StateName,
                  SutaBasis = s.SutaBasis ?? 0.00m,
                  ERRate = s.ERRate ?? 0.00m,
                  HighRate = s.HighRate ?? 0.00m,
                  PeoCode = p.PeoCode,
                  SutaReportingType = p.SutaReportingType,
                  SutaCost = p.SutaCost,
                  ColorValue = dc.ColorValue
              }).ToList();

这正是我所需要的。