2012-04-07 8 views
2

from..select linq 쿼리를 사용하여 XML 파일에서 데이터를 읽는 중 데이터가 매우 큽니다. 내가 읽을 수있는 가장 큰 데이터 세트는 1100 라인이며 각 라인은 100 문자로 구성됩니다. 이로 인해 휴대 전화가 멈추고 응용 프로그램이 다운됩니다. 에뮬레이터에서는로드하는 데 상당한 시간이 걸리지 만 정상적으로 작동합니다.조건에 따라 FROM..SELECT를 사용하여 Linq 요소를 선택하는 방법은 무엇입니까?

내가 원하는 것은 쿼리에 데이터 요소를 포함시키려는 몇 가지 조건을 기반으로합니다. 예를 들어, 내가 지금 가지고있는 것은 ...

var dataSet = from r in something.Elements("chapter") 
       select new dictChapter{ 
        Name = r.Attribute("Name").Value, 
        Desc1 = r.Attribute("Description1").Value, 
        Desc2 = r.Attribute("Description2").Value, 
        Desc3 = r.Attribute("Description3").Value 
       }; 
ListBox.DataContext = dataSet; 

그러나 설정에 따라 설명을 선택하고 싶습니다. 내가 좋아하는 뭔가 싶어 (나는 그것이 작동하지 않습니다 알고,하지만 난 내가하고 싶은 것을 설명합니다)

var dataSet = from r in something.Elements("chapter") 
       select new dictChapter{ 
        Name = r.Attribute("Name").Value, 
        if (ShowDesc1 == true) 
         Desc1 = r.Attribute("Description1").Value, 

        if (ShowDesc2 == true) 
         Desc2 = r.Attribute("Description2").Value, 

        if (ShowDesc3 == true) 
         Desc3 = r.Attribute("Description3").Value 
       }; 
ListBox.DataContext = dataSet; 
  1. 어떻게 C 샤프에이를 수 있습니까?
  2. 내 문제에 대해 더 좋은 해결책이 있습니까?

정말 감사합니다

답변

2

사용 conditional operator "?:"

var dataSet = from r in something.Elements("chapter") 
       select new dictChapter{ 
        Name = r.Attribute("Name").Value, 
        Desc1 = ShowDesc1 ? r.Attribute("Description1").Value : String.Empty, 
        Desc2 = ShowDesc2 ? r.Attribute("Description2").Value : String.Empty, 
        Desc3 = ShowDesc3 ? r.Attribute("Description3").Value : String.Empty, 

       }; 
+0

조건을 적용하는 방법을 알고 있으므로 부분적으로 내 질문에 답변 해 주셔서 감사합니다. 논리가 작동하지 않았습니다. 나는 여전히 같은 문제가있다. – wafers

2

당신은 같은 것을 시도 할 수 있습니다 :

var dataSet = from r in something.Elements("chapter") 
       select new dictChapter{ 
        Name = r.Attribute("Name").Value, 
        Desc1 = (ShowDesc1 ? r.Attribute("Description1").Value : null), 
        Desc2 = (ShowDesc2 ? r.Attribute("Description2").Value : null), 
        Desc3 = (ShowDesc3 ? r.Attribute("Description3").Value : null), 
       }; 

그것은 당신이 원하지 정확히 모든 Desc1-3 특성 때문에 모든 요소에 항상 설정되지만 ShowDesc1-3에 따라 false 인 경우 null로 설정됩니다.

+0

답변 해 주셔서 감사합니다. 당신 말이 맞아요! 내 문제를 해결하는 데 도움이되지 못했습니다. 다음에해야 할 일을 모릅니다! :) – wafers

관련 문제