c# linq to xml dynamic query

Posted by David Archer on Stack Overflow See other posts from Stack Overflow or by David Archer
Published on 2010-04-14T08:39:22Z Indexed on 2010/04/14 8:43 UTC
Read the original article Hit count: 395

Filed under:
|
|
|
|

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();

© Stack Overflow or respective owner

Related posts about c#

Related posts about linq-to-xml