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 = "";
}
只有列表中的索引正在改變。我怎樣才能使這個簡單?如何簡化此代碼?
你可以通過switch case語句來做到這一點,然後檢查是否ClassName =='p' || ClassName =='s',如果你不明白你的代碼,然後把它分解成更簡單的步驟。還詳細解釋了什麼問題和/或問題除了只是說裏面的列表正在改變 – MethodMan
謝謝建議,檢查底部的代碼 – Niewidzialny