Dynamic XLinq

  • Thread starter Thread starter acgritt
  • Start date Start date
A

acgritt

I am looking for some information on if it is possible/how to create a
dynamic XLinq statement. I know for Linq to SQL you can do the
following:

var query = db.Customers.
Where("City = @0 and Orders.Count >= @1", "London", 10).
OrderBy("CompanyName").
Select("new(CompanyName as Name, Phone)");

I am wondering how to do this with Linq to XML. I know I can do the
following:

var elements = doc.Descendants("nodeName").
Where(e => e.Attribute("attrib") == null ||
e.Attribute("attrib").Value != "value");

But I would like to do it more like the Linq to SQL if possible.

Does anyone have any information on this?

Adam
 
I wonder if in this instance XmlDocument and xpath (SelectNodes /
SelectSingleNode) aren't a better option?

I haven't done enough with XDocument to know whether this *also*
supports xpath (I'm sure somebody will jump in if it does...).

But then you'd be looking for something like (untested):

var nodes = SelectNodes("//nodeName[not(@attrib='value')]");

(where you can build that string via concatenation)

Marc
 
I wonder if in this instance XmlDocument and xpath (SelectNodes /
SelectSingleNode) aren't a better option?

I haven't done enough with XDocument to know whether this *also*
supports xpath (I'm sure somebody will jump in if it does...).

Yup, it does, with the XPathSelectElement, XPathSelectElement and
XPathEvaluate extension methods.

Jon
 
I am looking for some information on if it is possible/how to create a
dynamic XLinq statement.  I know for Linq to SQL you can do the
following:

var query = db.Customers.
     Where("City = @0 and Orders.Count >= @1", "London", 10).
     OrderBy("CompanyName").
     Select("new(CompanyName as Name, Phone)");

I am wondering how to do this with Linq to XML.  I know I can do the
following:

var elements = doc.Descendants("nodeName").
     Where(e => e.Attribute("attrib") == null ||
e.Attribute("attrib").Value != "value");

But I would like to do it more like the Linq to SQL if possible.

Does anyone have any information on this?

Yes. Google for "Dynamic LINQ" - it works with any LINQ provider
(since it just builds expression trees).
 
Yes. Google for "Dynamic LINQ" - it works with any LINQ provider
(since it just builds expression trees).

Thats what I found, however I am trying to find information on how to
distinguish between Attributes and Nodes if a string Where clause is
passed in. For instance in the following:

var query = db.Customers.
Where("City = @0 and Orders.Count >= @1", "London", 10).
OrderBy("CompanyName").
Select("new(CompanyName as Name, Phone)");

If City was an Attribute of the Customers nodes and Orders were child
nodes how would it look for XLinq.
 
Back
Top