문제

SQL에는 2 개의 테이블이 있습니다.

Table 1 
Step Id
Step Name

Table 2
Profile Id
Step Id
Completed

표 2에 일치하지 않더라도 다음 결과를 반환하고 싶습니다.

Results
Table1.Step Id
Table1.Step Name
Table2.Profile Id
Table2.Completed 

SQL 에서이 작업을 수행하는 방식은 다음과 같습니다.

select * from [Table 1] t1
left join [Table 2] t2
on t1.Step Id = t2.Step Id

이것은 내가 기대하는 결과를 생성합니다.

이것을 linq로 번역 할 때 :

public static List<UserCompletion> GetStepCompletion(string category, string profileid) { 

List<Step> step = GetSteps(category);
List<UserStep> userStep = GetUserSteps(category, profileId);    

var q = from s in step
         join us in userStep
         on s.Id equals us.StepId
         select new UserCompletion
         {
           StepId = s.Id,
           Headline = s.StepName,
           ProfileId = us.ProfileId
           Completed= us.Completed
          };

     return q.ToList();

}

작동하지만 왼쪽 조인이 아닌 조인처럼 작동합니다. 나는 단지 일치하는 결과를 얻습니다.

또한 userCompletion 은이 방법에서 반환하는 객체입니다.

나는 며칠 동안 이것에 대해 머리를 두드리고 있습니다 ... 어떤 도움도 감사 할 것입니다.

도움이 되었습니까?

해결책

당신은 또한 이것을 시도 할 수 있습니다 (us.completed가 부울이라고 가정) :

var q = from s in step
         let us = (from i in userStep where s.Id = i.StepId).SingleOrDefault()
         select new UserCompletion
         {
           StepId = s.Id,
           Headline = s.StepName,
           ProfileId = us.ProfileId
           Completed = us == null ? false : us.Completed
          };

이것은 SQL에 가입하지는 않지만 다음과 같은 중첩 된 선정 진술입니다.

select 
    StepId, Headline, ProfileId,
    isnull((select top(1) Completed from userStep where StepId = Id), 0) as Completed
from step

다른 팁

다음 줄을 따라 무언가를 시도하십시오.

var q = from s in step
        join us in userStep 
        on s.Id equals us.StepId into tmpjoin
        from x in tmpjoin.DefaultIfEmpty()
        select new UserCompletion { ... }

그것을 발견.

"가 될 수있는"항목에 대한 평가를 추가 해야하는 것 같습니다.

내 선택에 다음을 추가했습니다

Completed = (x == null) ? String.Empty : x.Completed 
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top