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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T05:42:17+00:00 2026-05-14T05:42:17+00:00

Right, bit of a strange question; I have been doing some linq to XML

  • 0

Right, bit of a strange question; I have been doing some linq to XML work recently (see my other recent posts here and here).

Basically, I want to be able to create a query that checks whether a textbox is null before it’s value is included in the query, like so:

XDocument db = XDocument.Load(xmlPath);
var query = (from vals in db.Descendants("Customer")
            where (if(textbox1.Text != "") {vals.Element("CustomerID") == Convert.ToInt32(textbox1.Text) } || 
                  if(textbox2.Text != "") {vals.Element("Name") == textbox2.Text})
            select vals).ToList();
  • 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-05-14T05:42:18+00:00Added an answer on May 14, 2026 at 5:42 am

    Just use the normal Boolean operators && and ||:

    XDocument db = XDocument.Load(xmlPath);
    var query = (from vals in db.Descendants("Customer")
                where (textbox1.Text != "" && 
                   vals.Element("CustomerID") == Convert.ToInt32(textbox1.Text)) || 
                   (textbox2.Text != "" && vals.Element("Name") == textbox2.Text)
                select vals).ToList();
    

    That’s just a direct translation of the original code – but I think you’ll want a cast from vals.Element("CustomerID") to int, and you don’t really want to convert textbox1.Text on every iteration, I’m sure. You also need to convert the “name” XElement to a string. How about this:

    int? customerId = null;
    if (textbox1.Text != "")
    {
        customerId = int.Parse(textbox1.Text);
    }
    
    XDocument db = XDocument.Load(xmlPath);
    var query = (from vals in db.Descendants("Customer")
                 where (customerId != null && 
                    (int) vals.Element("CustomerID") == customerId) ||
                    (textbox2.Text != "" && 
                     (string) vals.Element("Name") == textbox2.Text)
                 select vals).ToList();
    

    Alternatively, you could separate out the two parts of the query and “union” the results together. Or – preferably IMO – you could build the query more dynamically:

    var query = db.Descendants("Customer");
    if (textbox1.Text != null)
    {
        int customerId = int.Parse(textbox1.Text);
        query = query.Where(x => (int) x.Element("CustomerID") == customerId);
    }
    
    if (textbox2.Text != null)
    {
        query = query.Where(x => (string) x.Element("Name") == textbox2.Text);
    }
    List<XElement> results = query.ToList();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Right bit of a simple question can I input nText into a pivot table?
I'm trying to work with netbeans right now and I've run into a bit
I'm having a strange issue here, i have this bit of code: <?php if
I'm getting some strange errors with NAnt on my 64-bit Win 7 Box. Targeting
I'm playing around a bit and trying to get some things to work for
I have a bit of a strange problem. When an activity starts, I show
I'm a bit newbie to Flash, and I'm experiencing strange problem. I have a
Hi Guys this may seam a bit of a strange question but here goes:
This may be a bit of an odd question, and what I have in
Question may sounds strange but I want to make this question clear. Some people

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.