Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7729719
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T06:02:26+00:00 2026-06-01T06:02:26+00:00

I’m trying to find and collect all the elements that meet these requirements: var

  • 0

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!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T06:02:27+00:00Added an answer on June 1, 2026 at 6:02 am

    The problem is checkTest is IEnumerable<String>, not a string, so calling ToString() 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 checkTest

    So you should try something like:

    var match = prerequisite.Where(x => checkTest.Contains(x)).Select(item => item);
    
    foreach (var pr in match)
    {
        MessageBox.Show("this module has a prereq" + pr);
    }
    

    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 Module class and the Dictionary<String,Module>? If so, why are you doing this query?
    You already have everything you need to accomplish this without a query:

    var pr = modules[(String)listBox1.SelectedValue].Prerequisite;
    
    if (pr != "none")
        MessageBox.Show("this module has a prereq" + pr);
    

    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 checkTest query, you are selecting a list of Names that 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 checkTest with the follopwing:

    var checkTest = from d in xdoc.Descendants("Prerequisite")
                    where d.Value != "none"
                    select d.Parent.Element("Prerequisite").Value;
    

    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:

    var match = from x in prerequisite
                where checkTest.Contains(x)
                select x;
    

    the => operator is called a lamda expression. Try searching for LINQ query syntax and/or Lambda expressions to help understand the difference

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to find ID3V2 tags from MP3 file using jid3lib in Java.
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a text area in my form which accepts all possible characters from
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I'm trying to create an if statement in PHP that prevents a single post
Let's say I'm outputting a post title and in our database, it's Hello Y&#8217;all
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.