views:

65

answers:

2

Here is my dataset:

1 David
2 David
3 Marc
4 Marc
5 Marc
6 Marc
7 Marc
8 Marc
9 Marc
10 Marc
11 Marc
12 Marc
13 Marc
14 Marc
15 Marc

This query returns 2 records (correct):

    query = query.Where(Log => SqlMethods.Like
(Log.FormattedMessage, "%<key>Name</key><value>David</value>%"));

This query returns 2 records (correct):

    query = query.Where(Log => SqlMethods.Like
(Log.FormattedMessage, "%<key>Name</key><value>%David%</value>%"));

This query returns 0 records (correct):

    query = query.Where(Log => SqlMethods.Like
(Log.FormattedMessage, "%<key>Name</key><value>av</value>%"));

This query returns 2 records (correct):

    query = query.Where(Log => SqlMethods.Like
(Log.FormattedMessage, "%<key>Name</key><value>%av%</value>%"));

This query returns 0 records (correct):

    query = query.Where(Log => SqlMethods.Like
(Log.FormattedMessage, "%<key>Name</key><value>v</value>%"));

This query returns 15 records (incorrect, should return 2):

    query = query.Where(Log => SqlMethods.Like
(Log.FormattedMessage, "%<key>Name</key><value>%v%</value>%"));

What is wrong with the last query? Is it a bug or am I missing something?

A: 

Only reason I can see is, that you have %v% as filter, which means in regex (.+)v(.+), basically any value with a v somewhere in it. If this isn't the solution, can you post the results of the queries please?

Femaref
I appreciate your feedback but you're not addressing the whole filter. Its not just "%v%", its "%<key>Name</key><value>%v%</value>%". Additionally, there are only 2 records that have a "v" in them.
mcass20
+2  A: 
"%<key>Name</key><value>%v%</value>%"

What comes before/after key/value? For instance, I could imagine this matching it:

<key>Name</key><value> Stan</value>< v alue>Kyle </value>

BlueRaja - Danny Pflughoeft
Good point! I think you might be on to something! Now using your data example and my query structure, how would you query for "tan" so that it returned that record?
mcass20
@mcass: I'm not sure what you are asking - if you are asking how to parse XML with a LIKE query, [you can't](http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags). Really, what you want to do is parse the XML in code, and store the data in the database the way it's meant to be stored - using rows/columns. If you *really* must attempt to parse XML in SQL, SQL Server has some [rudimentary support](http://msdn.microsoft.com/en-us/library/ms345117%28SQL.90%29.aspx), though there is no way to access that indirectly from L2S.
BlueRaja - Danny Pflughoeft
@Blue: That's exactly what I'm trying to do. We dug ourselves a big hole when we tried to stuff a bunch of XML into a SQL field. It was our solution to the EntLib Logging App Block's "FormattedMessage" field. What a headache this has become getting the data back out! Thanks for saving me the time and pain of trying.
mcass20