Can LINQ to SQL query an XML field DB-serverside?

前端 未结 3 873
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-11 00:00

.NET 3.5, C#

I have a web app with a \"search\" feature. Some of the fields that are searchable are first-class columns in the table, but some of them are in fact n

3条回答
  •  [愿得一人]
    2020-12-11 00:27

    Now that is an interesting question.

    Right now, you cannot instruct SQL Server to perform XML functions directly from Linq. However, you can get Linq to use user defined functions... so, you could setup a udf to process the xml, get the right data, etc, and then use that in your Linq expresion. This will execute on the server and should do what you want. There's an important limitation, though: The XML path you're looking for (the first parameter to xmlColumn.value or similar) has to be built into the function because it has to be a string literal, it can't be built from an input parameter (for instance). So you can use UDFs for getting fields you know about when writing the UDF, but not as a general-purpose way to get data from XML columns.

    Check out the Supporting User Defined Functions (UDFs) section of Scott Gutherie's excellent Blog series on Linq to SQL for more info on implementation.

    Hope this helps.

提交回复
热议问题