2009-12-08 7 views
0

가 나는 문제는결합 LINQ는

var IDs = from p in ctx.bam_Zending_AllInstances 
          where p.Zender == "RT30" 
          select new { Identificatie = p.Identificatie }; 

       var latestIDs = from p in ctx.bam_Zending_AllInstances 
           where p.Zender == "RT30" 
           group p by p.Identificatie into q 
           select new { Identificatie = q.Key, Datum = q.Max(p => p.PrestatieOntvangen) }; 

       var final = from p in IDs 
          join q in latestIDs on p.Identificatie equals q.Identificatie 
          select new { Identificatie = p.Identificatie }; 

조인 싶어 LINQ 쿼리의 몇 가지를 쿼리합니다. 결과를 보면 ID에 308 개의 항목이 있고 최신 ID에는 304 개의 항목이 있지만 마지막 항목에는 308 개의 항목이 있습니다. 이것이 어떻게 가능한지? linq에서 조인이 내부 조인이라고 생각했습니다. 304 항목이 필요하지만 대신 외부 조인을 수행합니다.

내가 뭘 잘못하고 있니?

답변

0

ID가 실제로 유일하지 않았습니다.

var IDs = from p in ctx.bam_Zending_AllInstances 
          where p.Zender == "RT30" 
          select new { ID = p.Identificatie, Datum = p.PrestatieOntvangen }; 

       var latestIDs = from p in ctx.bam_Zending_AllInstances 
           group p by p.Identificatie into q 
           select new { ID = q.Key, Datum = q.Max(p => p.PrestatieOntvangen) }; 

       var final = from p in IDs 
          join q in latestIDs on new { p.ID, p.Datum } equals new { q.ID, q.Datum } 
          select new { ID = p.ID }; 
:

내가 가입 두 가지 조건을 사용하여 문제를 해결
1

여기에 고유 한 ID가 있습니까? 동일한 ID를 가진 두 개의 행이있는 경우 조인은 두 개 대신 네 개의 일치를 생성합니다.

최종 쿼리에 대해 생성 된 SQL을 살펴 보는 것이 좋습니다. 확실히 내부 조인이라고 기대할 수 있습니다.