2012-08-28 4 views
1

내에서 계산 될 수없는 I 다음 람다 식 있습니다 :이를 위해 작성 무엇을 중요하지 않습니다건너 뛰기는 람다 식

string queryToken = queryTokens.Last(); 
var result = from locationAddress in locations 
      let tokens = GetLetterTokens(locationAddress.Name) 
      let distance = (from token in tokens 
          where token.Contains(queryToken, StringComparison.OrdinalIgnoreCase) 
          select token.Length - queryToken.Length).Min() 
      orderby distance 
      select new 
         { 
          LocationAddress = locationAddress, 
          LocationDistance = distance, 
         }; 

합니다. 때로는 distance으로 계산하면 queryToken을 포함하는 tokens이 없으므로 .Min()을 반환 할 수 없습니다. 이 경우를 건너 뛰는 방법은 무엇입니까? 변수를 result 변수에 추가하고 싶지 않습니다.

답변

2

방금 ​​원하는 것 같은데 :

또는
let tokens = ... 
where tokens.Any(token.Contains(queryToken, StringComparison.OrdinalIgnoreCase)) 
let distance = ... 

, 당신은 토큰을 선택하면 바로 필터링 한 다음 어떤 존재 여부를 확인 : :)

var result = from locationAddress in locations 
      let tokens = GetLetterTokens(locationAddress.Name) 
           .Where(token => token.Contains(queryToken, StringComparison.OrdinalIgnoreCase) 
      where tokens.Any() 
      let distance = tokens.Min(token => token.Length - queryToken.Length) 
+0

감사 동료가 완벽하게 작동합니다! – Nickon