3

I am trying to figure out how to do a Linq To Entities query on a SQL Server Database which contains an XML datatype:

I want to do a database level query that returns only a specified XML node that contains over 2000 Characters

Is this possible in Linq To Entities?

Update

well i want to do something like this in linq-to-entities but I don't think its possible

var q = from c in Changes
 where c.Content.XPathSelectElement("OpsNotes").Value.Length >= 2000;
 select c;

inside the sql server database the XML content is a node called "OpsNotes"

 <Content Type="AP" Version="1">
  <Name>CPU FAIL-OVER</Name>
  <Bands>
    <Band>BAND DATA</Band>
  </Bands>
  <StartTime IsCritical="false" IsTimeSet="true">2009-12-06T14:00:00.0000000Z</StartTime>
  <Duration>00:30:00</Duration>
  <RecurrenceRule>RRULE:[List]{340/2009}</RecurrenceRule>
  <OpsNotes>Rationale: Support standard weekly DVIS CPU fail-over</OpsNotes>
   </Content>
2
  • Id be keen to know if you found and answer for this, if so would you mind post here Commented Sep 28, 2010 at 23:25
  • sorry did not.. I think the answers below would help - We decided to go with a rest based service to post xml data instead of directly accessing the Entity Framework layer.. this solved our issue. Commented Jan 13, 2011 at 14:52

2 Answers 2

1

I would be surprised if that were possible, directly.

You will most like need to do one of two workarounds:

  • retrieve the XML from the SQL Server field as a string, and put it into an XDocument (Linq-to-XML) or XmlDocument (DOM-based handling) and do your tricks on it

or:

  • handle the requests on SQL Server itself, using the SQL Server XQuery, and return results as string, ints etc. - whatever you need to do

I personally would prefer the option to handle the XQuery stuff on the server itself - in order to help you with your concrete problem, you'll have to provide some sample XML and an explanation of what you want to do....

Update: for your requirement, in SQL Server XQuery, you'd have to write something like:

var q = from c in Changes where c.Content.XPathSelectElement("OpsNotes").Value.Length >= 2000; select c;

SELECT (list of fields)
FROM dbo.Changes c
WHERE c.Content.value('(string-length(string((/Content/OpsNotes)[1])))', 'int') >= 2000

or something like that.

As an added bonus, in this case, you'd only transfer a smaller amount of rows back from the server to your client app since the server is already filtering out all the non-matching rows.

Sign up to request clarification or add additional context in comments.

1 Comment

Yeah - i think I will try that for now.. thanks! - seems like XML dataype should be supported by Linq-To-Entities
0

You'll have to write SQL for this.

In EF 4, you can use ExecuteStoreQuery to execute arbitrary SQL with entity-typed results.

1 Comment

hmm not using EF4 although i've got it on LinqPad 4 i think -i will investigate

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.