tags:

views:

62

answers:

2

Is there a way to cast an xml to a string in a Linq query. Something like this, but in LINQ:

select TOP(10) * from PackageSessionNodes
where CAST(Interactions as nvarchar(max)) like '%asset id%'
order by PackageSessionNodeId desc

This doesn't work:

var packageSessionNodes = from psn in db.PackageSessionNodes
                          where psn.Interactions.Contains("asset")
                          select psn;

Interactions is a XML Column in SQL2008 Server.

A: 

Wait, you mean like:

var packageSessionNodes = from psn in db.PackageSessionNodes
                          where psn.Interactions.Contains("asset")
                          select psn.ToString();

XML in Linq is represented by the XNode family of types. XNode.ToString() returns the xml representation of the node and its children.

Or, do you mean

var packageSessionNodes = from psn in db.PackageSessionNodes
                          where ((string)psn.Interactions).Contains("asset")
                          select psn;

assuming that psn is a type that has a public property Interactions which is an XElement. Of course, if it is, you should be parsing the XML instead of seeing if the xml text contains a specific string.

Will
I meant the second one. Well, parsing the xml in the linq query with linq to xml doesn't seem possible.
Lieven Cardoen
A: 

Have you tried:

var packageSessionNodes = from psn in db.PackageSessionNodes
                          where psn.Interactions.ToString().Contains("asset")
                          select psn

Depending on what Data Type SqlMetal decided to use for your XML Column (typically either XNode or XElement I believe), ToString() will return the XML as a String which should work for you.

Justin Niessner
Doesn't work. It's Linq to SubSonic, so maybe the problem is that SubSonic doesn't support it.
Lieven Cardoen