2013-01-09 1 views
0

이 항목이 있으며 기간은 2 일 때 원하는 항목을 선택할 수 있습니다.하지만 기간 2가 없으면 기간 3을 선택하려고합니다. 방법?특성이 x 인 경우 하위 항목을 선택하고 속성이 y를 선택하십시오.

.Where(node => (string)node.Attribute("period") == "2" || (string)node.Attribute("period") == "3") 

:

return document.Descendants("time") 
         .Where(node => (string)node.Attribute("period") == "2") 
         .Take(5) 
         .Select(status => WeatherFactory.Create(status, city, pos)) 
         .ToList(); 
+1

이것 하나의 쿼리? –

답변

1

이 모든 자손을 선택하지 않을 경우 기간 == 2, 그것은 어떤 결과가 포함되어 있는지 확인하고 어디 먼저 모든 하위 항목을 선택하면 어디 기간 == 3 : 당신이해야 할 이유

var timeDescendants = document.Descendants("time") 
    .Where(node => (string)node.Attribute("period") == "2"); 
if(!timeDescendants.Any()) { 
    timeDescendants = document.Descendants("time") 
     .Where(node => (string)node.Attribute("period") == "3"); 
} 
return timeDescendants.Take(5) 
.Select(status => WeatherFactory.Create(status, city, pos)) 
.ToList(); 
+0

그게 정확히 내가 찾고 있던 것입니다! 감사합니다. –

3

당신은 단지에 어디를 변경할 수 없습니다?

+0

하지만 실제로. 이제는 두 가지를 선택합니다. :( –

관련 문제