2016-11-13 1 views
-2
if (spanList.Count(p => p.ClassName == "p") == 2 && (spanList.Count(p => p.ClassName == "s") == 2)) 
{ 
    lesson.lesson2Name = spanList.Where(p => p.ClassName == "p").ToList()[1].TextContent; 
    lesson.lesson2Place = spanList.Where(p => p.ClassName == "s").ToList()[1].TextContent; 
    lesson.lesson2Tag = adressList.Where(p => p.ClassName == "n").ToList()[1].TextContent; 
    lesson.lesson2TagHref = adressList[1].GetAttribute("href"); 
} 
else if (spanList.Count(p => p.ClassName == "p") == 4 && (spanList.Count(p => p.ClassName == "s") == 2)) 
{ 
    lesson.lesson2Name = spanList.Where(p => p.ClassName == "p").ToList()[2].TextContent; 
    lesson.lesson2Place = spanList.Where(p => p.ClassName == "s").ToList()[1].TextContent; 
    lesson.lesson2Tag = spanList.Where(p => p.ClassName == "p").ToList()[3].TextContent; 
    lesson.lesson2TagHref = ""; 
} 

목록의 색인 만 변경됩니다. 어떻게하면 더 간단하게 만들 수 있습니까?이 코드를 더 간단하게 만드는 방법은 무엇입니까?

+1

스위치 케이스 문을 사용하면 훨씬 쉽게이 작업을 수행 할 수 있습니다. ClassName =='p' || ClassName =='s' 당신이 코드를 이해하지 못한다면 더 간단한 단계로 나누십시오. 또한 세부 사항에 대해 더 자세히 설명해주십시오. 단지 내부 목록 만 변경하는 것 외에는 – MethodMan

+0

덕분에 코드를 확인하십시오. 하단의 – Niewidzialny

답변

1

성능 및 가독성 측면에서보기에 나쁘다. 사용자가 뭔가를 알고 싶을 때마다 전체 목록을 반복하므로.

당신은

List<YourObject> pList = spanList.Where(p => p.ClassName == "p").ToList(); 
List<YourObject> sList = spanList.Where(p => p.ClassName == "s").ToList(); 
if (pList.Count == 2 && sList.Count == 2) 
{ 
    lesson.lesson2Name = pList[1].TextContent; 
    lesson.lesson2Place = sList[1].TextContent; 
    lesson.lesson2Tag = adressList.Where(p => p.ClassName == "n").ToList()[1].TextContent; 
    lesson.lesson2TagHref = adressList[1].GetAttribute("href"); 
} 
else if (pList.Count == 4 && sList.Count == 2)) 
{ 
    lesson.lesson2Name = pList[2].TextContent; 
    lesson.lesson2Place = sList[1].TextContent; 
    lesson.lesson2Tag = pList.ToList()[3].TextContent; 
    lesson.lesson2TagHref = ""; 
} 
+0

코드를 확인하십시오. 그리고이 문제를 생각해 낼 수 없다고 생각하십시오. 고마워, 내가 어떻게 그것을 그리워했는지 모르겠다. 또한 더 간단하게 할 수는 없을까요? – Niewidzialny

+0

하단의 답변 확인 – Niewidzialny

0
var pList = spanList.Where(p => p.ClassName == "p").ToList(); 
var sList = spanList.Where(p => p.ClassName == "s").ToList(); 

if(sList.Count == 2) 
{ 
    string name = ""; 
    string tag = ""; 
    string taghref = ""; 

    switch(pList.Count) 
    { 
     case 2: 
      name = pList[1].TextContent; 
      tag = adressList.Where(p => p.ClassName == "n").ToList()[1].TextContent; 
      taghref = adressList[1].GetAttribute("href"); 
      break; 
     case 4: 
      name = pList[2].TextContent; 
      tag = pList[3].TextContent; 
      break; 
     default: 
      name = "error"; 
      break; 
    } 

    lesson.lesson2Name = name; 
    lesson.lesson2Place = sList[1].TextContext; 
    lesson.lesson2Tag = tag; 
    lesson.lesson2TagHref = taghref; 
} 

@Crusha K. Rool을 MethodMan가 말한대로 내가 switch 문을 사용하는 식으로 뭔가를 시도하고이를 얻어야한다. 더 잘할 수 있습니까?

+0

원래 질문을이 게시물로 업데이트해야하는 이유는 무엇입니까? – MethodMan

+2

나는 피할 수 있다면 보통 쓸데없는 숙제의 팬이 아니다. 나는'string name;'선언을'= "";'부분없이 그냥 거기에 놓았습니다. switch 문에있는 모든 경우는 원래의 할당이 사용되지 않도록'name'에 값을 할당합니다. 다른 변수에 대해서도 일찍 처리하는 대신 각 사례의 일부로 기본 할당을 추가하는 방법으로 동일하게 수행 할 수 있습니다. 그러나 결국 그것은 개인적 선호로 귀결됩니다. –

+0

@MethodMan 내 사이트에서 no-ogar를 유감스럽게 생각합니다. stackoverflow의 첫 단계를 수행하고 있습니다. 나는 이것을 기억할 것이다. 답장을 보내 주셔서 감사합니다. 나는 네가 말한 것처럼 코딩 할 것이다. – Niewidzialny

관련 문제