keyongtech


  keyongtech > dotnet.languages.* > dotnet.languages.csharp > 07/2008

 #1  
07-11-08, 10:01 AM
timor.super
Hi group,

In my database, I have a table with fields like this :

id | title | xml
------------------------------------
1 | title1 | <datas><data><item name="item1">value1</
item><item name="item2">value2</item></data></datas>
......

I would like to filter my xml data, when it contains a item attribute
named item1

I'm using Linqpad with a request like that :

from i in myTable
where
i.xml.Descendants("data").Element("item").Attribut e("name").Value ==
"item1"
select i

but, linqpad tells me that :

'System.Collections.Generic.IEnumerable<System.Xml .Linq.XElement>'
does not contain a definition for 'Element' and no extension method
'Element' accepting a first argument of type
'System.Collections.Generic.IEnumerable<System.Xml .Linq.XElement>'
could be found (press F4 to add a using directive or assembly
reference)

Is this possible ? How to correct it ?

Thanks in advance for any help
 #2  
07-11-08, 11:05 AM
Jeroen Mostert
timor.super wrote:
[..]
>
> but, linqpad tells me that :
>
> 'System.Collections.Generic.IEnumerable<System.Xml .Linq.XElement>'
> does not contain a definition for 'Element' and no extension method
> 'Element' accepting a first argument of type
> 'System.Collections.Generic.IEnumerable<System.Xml .Linq.XElement>'
> could be found (press F4 to add a using directive or assembly
> reference)
>

It's telling you that "Descendants" will give you a collection of elements,
and you're trying to call "Element" on this collection (which only applies
to single elements).

This would work:

where i.xml.Descendants("data").Descendants("item").Any( item => (string)
item.Attribute("name") == "item1")

So would this (using XPath):

where i.xml.XPathSelectElements("/data/item[@name=\"item1\"]").Any()
 #3  
07-11-08, 12:05 PM
Jeroen Mostert
Jeroen Mostert wrote:
> timor.super wrote:
> It's telling you that "Descendants" will give you a collection of
> elements, and you're trying to call "Element" on this collection (which
> only applies to single elements).
>
> This would work:
>
> where i.xml.Descendants("data").Descendants("item").Any( item =>
> (string) item.Attribute("name") == "item1")
>
> So would this (using XPath):
>
> where i.xml.XPathSelectElements("/data/item[@name=\"item1\"]").Any()
>

....except that neither of these work if "myTable" is an actual SQL table,
because LINQ to SQL can't handle XML methods. This is quite unfortunate,
because SQL Server 2005 does have support for querying XML. You can get
around it by materializing the query results and working with that, of course:

from i in myTable.ToArray()
where i.xml.XPathSelectElements("/data/item[@name=\"item2\"]").Any()
select i

But this has the huge drawback of pulling in all the records for selecting
on the client side, which rather defeats the point of querying.

If your table will never become big enough for this to matter, it might be
acceptable. Otherwise, you could factor out the XML selection logic to the
database. For example:

CREATE FUNCTION dbo.SelectByItemName(@name AS NVARCHAR(MAX))
RETURNS TABLE
RETURN
SELECT id, title, xml FROM myTable
WHERE xml.exist('/datas/data/item[@name=sql:variable("@name")]') = 1;

You can then use LINQ to XML in the designer to create a mapping to this
function.

The obvious drawback to this method is that you need to carefully work out
your data needs in advance as far as the SQL selection is concerned. And if
you can do that, you may want to consider avoiding XML altogether and using
a pure relational database, which cooperates much more nicely with most data
access technologies.
Similar Threads
Question about LinQ (LinQ to Sql)

I have already heard that LinQ to Sql will be replaced by LinQ to Entities or something like that. My question is below: that the whole concept of LinQ whatever -- it is...

LINQ???

I don't have my book with me today... but I thought I saw a sample in the book that .. I have one dataset.. two tables.... columns are identicaly.. with the exception of a...

Linq to XML--Are there code examples that make Linq as easy as SQL? Or how can I convert ths simple pseudo code into real code?

Maybe I shouldn't be using XML for what I want. I want to store some program data and retrieve it easily, iterating through elements grabbing child elements easily and...

Combination of Linq to sql and Linq to xml

Hi folks I am pretty new to linq and in one of my first samples I tried to do the following: In my sample-database I have a table which has some columns and one of them has...

Linq to SQL - Return DataTable as a result of Linq query

Hi! How to get result od dataTable from Linq query? I have typied DataSet and I want to join couple of tables. And I have a problem with change this result to DataTable...


All times are GMT. The time now is 02:38 AM. | Privacy Policy