How can I use "like" in linq for xml

I want to do something like this. I know it wrong:

 var a = from h in xdoc.Root.Elements()
         where h.Element().value like = "1234"
         select h;

      

+2


source to share


4 answers


var a = from h in xdoc.Root.Elements()
        where h.Element.value.Contains("1234") 
        select h

      



This will create a "LIKE" statement in the background.

+7


source


I think you want to get the elements Contains

value 1234

:

var a = from h in xdoc.Root.Elements()
         where h.Element().Value.Contains("1234") // like '%1234%'
         select h;

      



For SQL-ish like '%value'

you can use EndsWith and for like 'value%'

StartsWith

+6


source


Use the String class helper methods such as StartsWith

or EndsWith

.

+1


source


Here I am using the StartsWith () method to do the same.

var CountryNames = from city in xdoc.Descendants("countries").Elements("city")                         
                           where city.Value.StartsWith(prefixText)
                           select city.Value;

      

Contains

EndsWith

StartsWith

0


source







All Articles