Filtering database records by xml column content using linq-to-sql and xlinq

I need to select rows from a database table using column filtering of type XML.

Table

looks like (short version)

id
dbfield int
xmlfield xml

and I filter it this way

IQueryable<Data.entity> q = from u in datacontex.entities
select u;

if (val1.HasValue)
  q = q.Where( x => x.dbfield > val1.value)

if (val2.HasValue)
  q = q.Where( x=> x.dbfield < val2.value)

if (!string.IsNullOrEmpty(searchString))
 q = q.Where ( x=> x.xmlfield contains values from searchString)

XML in xmlfield very simply looks like

<doc>
  <item id="no">test/WZ/2009/04/02</item>
  <item id="title">blabla</item>
...

The question is how to add the WHERE clause in linq and, preferably, this conversion should translate to the ms-sql query without processing the dataset in the webservice application.

Thanks.

+3
source share
2 answers

LINQ-to-SQL does not support AFAIK xml extensions in TSQL. Two options that I see:

  • SPROC/udf , sql/xml,
  • UDF, ( bool), ctx.SomeUdf(row) where LINQ
+3

SQL Server, XML , "" . - .

, , Linq-to-SQL - .

+1

Source: https://habr.com/ru/post/1722974/


All Articles