2016-07-02 4 views
2

가 의미를 쿼리 Document에 :쓰기 내가 계산하기 위해 다음 코드를 쓴

if(currentStationId == 1) ReferCounts = x.DocumentStationHistories 
             .Count(t => t.ToStationId == currentStationId && 
                t.FromStationId != t.ToStationId)) 
else 
    ReferCounts = x.DocumentStationHistories 
        .Count(t => t.ToStationId == currentStationId)) 

어떻게 :

var currentStationId = GetCurrentStation(); 
var result = dbContext 
      .Documents 
      .Select(x=> 
        new MyDTO{ 
          ..., 
          ReferCounts = x.DocumentStationHistories 
              .Count(t => t.ToStationId == currentStationId)) 
          } 

는 지금은 ReferCounts을 계산하는 메커니즘을 변경하려면 엔티티 쿼리에 내 linq 에서이 코드를 사용합니까?

+0

[ternary if] (https://msdn.microsoft.com/en-us/library/ty67wk28.aspx) 구문은 어떻습니까? – nlloyd

+0

형식 변경 : (currentStationId == 1)? ReferCounts = x.DocumentStationHistories .Count는 (t => t.ToStationId ==는 && t.FromStationId을 currentStationId = t.ToStationId!)) : 의 ReferCounts = x.DocumentStationHistories의 .Count (t => t.ToStationId == currentStationId)) – jdweng

답변

0

다음은 ternary if 구문의 예입니다.

var result = dbContext 
     .Documents 
     .Select(x=> 
       new MyDTO{ 
         ..., 
         ReferCounts = (currentStationId == 1) ? 
          x.DocumentStationHistories.Count(t => t.ToStationId == currentStationId && t.FromStationId != t.ToStationId)) 
          : x.DocumentStationHistories.Count(t => t.ToStationId == currentStationId)) 
         } 
관련 문제