Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Just use the normal Boolean operators &amp;&amp; and ||:</p> <pre><code>XDocument db = XDocument.Load(xmlPath); var query = (from vals in db.Descendants("Customer") where (textbox1.Text != "" &amp;&amp; vals.Element("CustomerID") == Convert.ToInt32(textbox1.Text)) || (textbox2.Text != "" &amp;&amp; vals.Element("Name") == textbox2.Text) select vals).ToList(); </code></pre> <p>That's just a direct translation of the original code - but I think you'll want a cast from <code>vals.Element("CustomerID")</code> to <code>int</code>, and you don't really want to convert <code>textbox1.Text</code> on every iteration, I'm sure. You also need to convert the "name" <code>XElement</code> to a string. How about this:</p> <pre><code>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 &amp;&amp; (int) vals.Element("CustomerID") == customerId) || (textbox2.Text != "" &amp;&amp; (string) vals.Element("Name") == textbox2.Text) select vals).ToList(); </code></pre> <p>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:</p> <pre><code>var query = db.Descendants("Customer"); if (textbox1.Text != null) { int customerId = int.Parse(textbox1.Text); query = query.Where(x =&gt; (int) x.Element("CustomerID") == customerId); } if (textbox2.Text != null) { query = query.Where(x =&gt; (string) x.Element("Name") == textbox2.Text); } List&lt;XElement&gt; results = query.ToList(); </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload