I’m trying to find and collect all the elements that meet these requirements:
var checkTest = from d in document.Descendants("Prerequisite")
where d.Value != "none"
select d.Parent.Element("Name").Value;
by using this foreach loop:
foreach (var item in prerequisite)
{
if (item == checkTest.ToString())
{
MessageBox.Show("this module has a prereq" + item);
}
}
but it won’t do anything for some reason, anybody know why? I think the problem is in the var checkTest
(this is what prerequisite means in the foreach:)
var prerequisite = from d in document.Descendants("Name")
where d.Value == (String)listBox2.SelectedItem
select d.Parent.Element("Prerequisite").Value;
Here is the XML:
<?xml version="1.0" encoding="utf-8" ?>
<SoftwareEngineering>
<Module>
<Name>Algorithms and Data Structures</Name>
<Code>3SFE504</Code>
<Capacity>5</Capacity>
<Semester>1</Semester>
<Prerequisite>none</Prerequisite>
</Module>
<Module>
<Name>3D Graphics I</Name>
<Code>3SFE508</Code>
<Capacity>5</Capacity>
<Semester>1</Semester>
<Prerequisite>none</Prerequisite>
</Module>
<Module>
<Name>Event-Driven Programming</Name>
<Code>3SFE513</Code>
<Capacity>10</Capacity>
<Semester>1</Semester>
<Prerequisite>none</Prerequisite>
</Module>
<Module>
<Name>Object Oriented Design</Name>
<Code>3SFE514</Code>
<Capacity>10</Capacity>
<Semester>1</Semester>
<Prerequisite>none</Prerequisite>
</Module>
<Module>
<Name>Requirements Engineering</Name>
<Code>3SFE516</Code>
<Capacity>10</Capacity>
<Semester>1</Semester>
<Prerequisite>none</Prerequisite>
</Module>
<Module>
<Name>Introduction to AI</Name>
<Code>3SFE599</Code>
<Capacity>5</Capacity>
<Semester>1</Semester>
<Prerequisite>none</Prerequisite>
</Module>
<Module>
<Name>Java Mobile Application Development</Name>
<Code>3SFE540</Code>
<Capacity>5</Capacity>
<Semester>1</Semester>
<Prerequisite>3SFE514(corequisite)</Prerequisite>
</Module>
<Module>
<Name>C# .NET Programming</Name>
<Code>3SFE541</Code>
<Capacity>5</Capacity>
<Semester>1</Semester>
<Prerequisite>3SFE514(corequisite)</Prerequisite>
</Module>
<Module>
<Name>Software Engineering Group Project</Name>
<Code>3SFE515</Code>
<Capacity>5</Capacity>
<Semester>2</Semester>
<Prerequisite>3SFE514(corequisite)</Prerequisite>
</Module>
<Module>
<Name>Software Engineering</Name>
<Code>3SFE519</Code>
<Capacity>10</Capacity>
<Semester>2</Semester>
<Prerequisite>none</Prerequisite>
</Module>
<Module>
<Name>Mobile User Interface Development</Name>
<Code>3SFE542</Code>
<Capacity>5</Capacity>
<Semester>2</Semester>
<Prerequisite>3SFE540</Prerequisite>
</Module>
<Module>
<Name>Interactive Multimedia</Name>
<Code>3MTS954</Code>
<Capacity>5</Capacity>
<Semester>2</Semester>
<Prerequisite>none</Prerequisite>
</Module>
<Module>
<Name>Concurrent Programming</Name>
<Code>3SFE555</Code>
<Capacity>5</Capacity>
<Semester>2</Semester>
<Prerequisite>none</Prerequisite>
</Module>
<Module>
<Name>Mobile Gaming</Name>
<Code>3SFE557</Code>
<Capacity>10</Capacity>
<Semester>2</Semester>
<Prerequisite>none</Prerequisite>
</Module>
<Module>
<Name>Intelligent Systems</Name>
<Code>3SFE500</Code>
<Capacity>10</Capacity>
<Semester>2</Semester>
<Prerequisite>3SFE599</Prerequisite>
</Module>
<Module>
<Name>3D Graphics II</Name>
<Code>3SFE501</Code>
<Capacity>10</Capacity>
<Semester>2</Semester>
<Prerequisite>3SFE508</Prerequisite>
</Module>
</SoftwareEngineering>
Does it have to do with my selecting an upper-sibling when having searched for its lowest sibling? i.e. I searched for Prerequisite first and then said to select its Name.Value (in checkTest)
Even when I try and make the if statement as !=, it will print each prerequisite, even when its none!
The problem is
checkTestisIEnumerable<String>, not a string, so callingToString()is not providing a good value to compare too. FYI –IEnumerable<T>is the return value of all basic LINQ querys, although there are ways to extract the first or last elements.It looks like you are trying to get a list of prerequisites that match one of the elements in
checkTestSo you should try something like:
EDIT: updated answer based on previous question by OP
From the answer I gave you yesterday morning on a similar topic, are you using the
Moduleclass and theDictionary<String,Module>? If so, why are you doing this query?You already have everything you need to accomplish this without a query:
2nd Edit to answer actual LINQ query question
My previous edit suggesting going back to the previous questions and suing the preloaded XML and collection is still a better solution, but since your questions seem to go back to using LINQ-to-XML, you obviously have a reason to prefer it.
I took a more detailed look at your question and i think i have the problem. You are comparing 2 elements that will never match. In your
checkTestquery, you are selecting a list ofNamesthat have a prereq but you are trying to compare to a list of prereq’s that only contain a code.You should use my query that I provided to check for matches, but you should get
checkTestwith the follopwing:EDIT #3 – Rewriting with LINQ Query syntax
To respond to your last comment, the query I provided you is an alternative form of LINQ query that I tend to prefer to smaller/shorter queries.
You could rewrite that query using the query syntax you have been using as:
the => operator is called a lamda expression. Try searching for LINQ query syntax and/or Lambda expressions to help understand the difference