เลือก XElements ที่ไม่ซ้ำกัน (ตามคุณลักษณะ) โดยใช้ LinqToXml

ฉันมี XML ที่ฉันมีแท็ก <testcase> หลายแท็ก บางส่วนมีแอตทริบิวต์ "ident" ที่มีค่า "pr" บางส่วนไม่มี

<testcase>
  <teststep ident="Preparation" result="na">blabla</teststep>
  <teststep ident="" result="pass">blabla</teststep>
  <teststep ident="-" result="na">blabla</teststep>
  <teststep ident="Info" result="na">blabla</teststep>
  <teststep ident="1" result="pass">blabla</teststep>
  <teststep ident="2" result="pass">blabla</teststep>
  <teststep ident="3" result="pass">blabla</teststep>
  <teststep ident="4" result="fail">blabla</teststep>
  <teststep ident="PR" result="na">blabla</teststep>
  <verdict result="fail" />
</testcase>

ฉันต้องการสอบถาม testcases ซึ่งมีแอตทริบิวต์ ident ที่มีค่า "pr"

IEnumerable<XElement> failedPRTCs = report.Descendants("testcase").Where(t => t.Element("verdict").Attribute("result").Value == "fail" && t.Descendants("teststep").Where(ts=> ts.Attribute("ident").Value == "pr").ToList().Count > 0).ToList();

แบบสอบถามปัจจุบันส่งคืน 0 มีปัญหาอะไร


person Juri Bogdanov    schedule 11.09.2012    source แหล่งที่มา


คำตอบ (1)


ดูเหมือนปัญหาง่ายๆ ของเคส! "pr" กับ "PR"! ฉันอยากจะแนะนำให้ใช้ Any มากกว่า count > 0:

IEnumerable<XElement> failedPRTCs = report.Descendants("testcase")
    .Where(t => t.Element("verdict").Attribute("result").Value == "fail" &&
           t.Descendants("teststep").Any(ts=> ts.Attribute("ident").Value == "PR"));
person ColinE    schedule 11.09.2012
comment
ใช่ขอบคุณ :) ฉันทำผิดพลาดง่าย ๆ จริงๆ .. pr ตัวพิมพ์เล็กแทน PR และขอขอบคุณสำหรับคำแนะนำเกี่ยวกับคำหลักใดๆ - person Juri Bogdanov; 11.09.2012