locked
LINQ to Entities with string comparison? RRS feed

  • Question

  • I have some SQL that I cannot figure out how to translate to LINQ. Specifically the SQL compares values against a string filter using the string comparison functions in MySQL, but there does not appear to be any way to compare string values with anything except equality operators in LINQ? For instance, if we have the following SQL:

    select name
    from products p
    where name >= 'startFilter' and 
         name <= 'endFilter'
    

    there appears to be now way to convert this to LINQ? Obviously in the above SQL, the products.name column is a string value. Is there any way to do this?

     

    Wednesday, July 6, 2011 1:37 AM

Answers

  • Hi,

    You could try to use the string class CompareTo method, this will be converted to <= and >=

    eg.

    .Where(row => row.Name.CompareTo("Comparestring") > 0)

    will be converted to

    Where Name > 'Comparestring'


    --Rune
    Wednesday, July 6, 2011 1:42 PM

All replies

  • Hi,

    You could try to use the string class CompareTo method, this will be converted to <= and >=

    eg.

    .Where(row => row.Name.CompareTo("Comparestring") > 0)

    will be converted to

    Where Name > 'Comparestring'


    --Rune
    Wednesday, July 6, 2011 1:42 PM
  • Ok great, I will give that a try! I had thought about using those but I was not sure if they would convert properly, as they are not listed in the MSDN documentation as Canonical functions that will convert over to SQL:

    http://msdn.microsoft.com/en-us/library/bb738681.aspx


    Wednesday, July 6, 2011 5:38 PM
  • Strange it isn't documented there...

    But, it may not work on other DBMSes other than SQL Server... I don't know..


    --Rune
    Wednesday, July 6, 2011 8:31 PM
  • Works great, thanks!
    Friday, July 8, 2011 7:45 PM
  • Great. It's work
    Wednesday, April 15, 2015 12:05 PM