title: Querying SQL Server xml column with user provided XPath via Entity Framework
body: <p>I'm having a really tough time figuring how to use an xml data column in SQL Server, specifically for use with Entity Framework.</p>
  
  <p>Basically, one of our tables stores "custom metadata" provided by users in the form of XML, so it seemed sensible to store this in an Xml column in the table.</p>
  
  <p>One of the requirements of our application is to support searching of the metadata, however.  The users are able to provided an XPath query string, as well as a value to compare the value of the XPath with, to search for elements that contain metadata that matches their query.</p>
  
  <p>I identified the SQL Server xml functions as ideal for this (eg, [xmlcol].exist('/path1/path2[0][text()=''valuetest'''] ), but they're not supported by Entity Framework, irritatingly (or specifically, xml columns aren't supported).  As an alternative, I tried creating a UDF that passes the user-provided XPath to the xml functions, but then discovered that the xml functions only allow string literals, so I can't provide variables...</p>
  
  <p>At this point, I was running out of options.</p>
  
  <p>I created a small bit of code that performs a regular expression replace on the result of a IQueryable.ToString(), to inject my XPath filter in, and then send this string to the database manually, but there are problems with this too, such as the result doesn't seem to lazily load the navigational properties, for example.</p>
  
  <p>I kept looking, and stumbled upon the idea of SQLCLR types, and started creating a SQLCLR function that performs the XPath comparison.  I thought I was onto a winner at this point, until a colleague pointed out that SQL Server in Azure doesn't support SQLCLR - doh!</p>
  
  <p>What other options do I have? I seem to be running very close to empty...</p>