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 9169461
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T15:52:56+00:00 2026-06-17T15:52:56+00:00

I am using following linq query to work with XML file.. XElement rootElement =

  • 0

I am using following linq query to work with XML file..

XElement rootElement = XElement.Load(@"test.xml"); 
int StyCode; 
var lv1s = from lv1 in rootElement.Descendants("Class") 
where lv1.Attribute("Code").Value.Equals("002") 
select new 
{ 
Children = from ltd in lv1.Descendants("Subject") 
where ltd.Attribute("Course").Value.Equals("Math") 
select new 
{ 

****//In This below section need result on condition based... ****
if(StyCode=0)  
{
Children1 = ltd.Attribute("AllTeachers").Value.Equals("Y") ? true : false
}
else
{
Children2 = ltd.Attribute("SpeciaGuest").Value.Equals("Y") ? true : ltd.Elements("Topic").Attributes("Code").Where(x => x.Value.Equals("1")).FirstOrDefault() != null ? true : false 
}
} 
}; 

Below is my XML Structure..

<?xml version="1.0" encoding="utf-8" ?>
<Document>
  <Class Code="001">
    <Subject Course="Math" AllTeachers='N' SpeciaGuest='N'></Subject>
    <Subject Course="Engish" AllTeachers='Y' SpeciaGuest='Y'></Subject>
    <Subject Course="History" AllTeachers='Y' SpeciaGuest='Y'></Subject>
  </Class>
  <Class Code="002">
    <Subject Course="Math" AllTeachers='Y' SpeciaGuest='N'>
      <Topic Code="1">LAW1</Topic>
      <Topic Code="2">
        LAW2
      </Topic>
      <Topic Code="3">
        LAW3</Topic>
      </Subject>
    <Subject Course="Engish" AllTeachers='Y' SpeciaGuest='Y'></Subject>
    <Subject Course="History" AllTeachers='Y' SpeciaGuest='Y'></Subject>
  </Class>
</Document>

Please let me know, how I can use condition to select multiple select result. Also let me know using resharper in this linq query will be fine?.

  • 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-17T15:52:57+00:00Added an answer on June 17, 2026 at 3:52 pm

    Because if(StyCode=0) isn’t based on the current item in the sequence, you can pull the if outside of the query:

    Also note that because both of the Select clauses are selecting a single item, you don’t need an anonymous type, just select that item. That results in this:

    XElement rootElement = XElement.Load(@"test.xml");
    int StyCode = 0;
    IEnumerable<IEnumerable<bool>> lv1s;
    if (StyCode == 0)
    {
        lv1s = from lv1 in rootElement.Descendants("Class")
                where lv1.Attribute("Code").Value.Equals("002")
                select (from ltd in lv1.Descendants("Subject")
                        where ltd.Attribute("Course").Value.Equals("Math")
                        select ltd.Attribute("AllTeachers").Value.Equals("Y") ? true : false);
    }
    else
    {
        lv1s = from lv1 in rootElement.Descendants("Class")
                where lv1.Attribute("Code").Value.Equals("002")
                select (from ltd in lv1.Descendants("Subject")
                        where ltd.Attribute("Course").Value.Equals("Math")
                        select ltd.Attribute("SpeciaGuest").Value.Equals("Y") ? true : ltd.Elements("Topic").Attributes("Code").Where(x => x.Value.Equals("1")).FirstOrDefault() != null ? true : false);
    }
    

    So this is simple, and nice, but has a lot of code duplication, which is not no nice. Another road to travel down is to create a method that takes an XElement that represents ltd as well as an integer StyCode and returns a boolean indicating the appropriate value. It’s a simple enough method to write:

    private static bool GetChildFromSubject(int styCode, XElement subject)
    {
        if (styCode == 0)
            return subject.Attribute("AllTeachers").Value.Equals("Y");
        else
        {
            return subject.Attribute("SpeciaGuest").Value.Equals("Y") ||
                    subject.Elements("Topic").Attributes("Code")
                    .Any(x => x.Value.Equals("1"));
        }
    }
    

    Now we only need one query:

    XElement rootElement = XElement.Load(@"test.xml");
    int StyCode = 0;
    var lv1s = from lv1 in rootElement.Descendants("Class")
                where lv1.Attribute("Code").Value.Equals("002")
                select (from ltd in lv1.Descendants("Subject")
                        where ltd.Attribute("Course").Value.Equals("Math")
                        select GetChildFromSubject(StyCode, ltd));
    

    Much better.

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

Sidebar

Related Questions

I'm using LINQ-to-Entities. Using the following query: var x = from u in context.Users
I am using LINQ to EF and have the following LINQ query: var results
I'm working on the following LINQ query: public void GetAuditRuleAgencyRecords(IEnumerable<Entities.AuditRule> rules) { using (LinqModelDataContext
I ran into an interesting error with the following LiNQ query using LiNQPad and
I have the following LINQ to entities query (using odp.net) : Dim query =
I'm trying to get the following SQL query to work in LINQ: Select id
Suppose following codes: IEnumerable<MyClass> MakeQuery() { var query = from m in session.Linq<MyClass>() select
I am following Scott Gu's article to create a dynamic LINQ http://weblogs.asp.net/scottgu/archive/2008/01/07/dynamic-linq-part-1-using-the-linq-dynamic-query-library.aspx He has
I have following LINQ statement and I want to rewrite it using extension methods.
I am using the following pattern to update my SQL Server records using Linq

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.