1

I want to do a query in C# (EF Core and LINQ), where the query is structured like this:

SELECT someField
  FROM SomeTable
  WHERE someField >= 'A 100' AND someField <= 'A 150'

The SQL works just fine, but I cannot find a way to translate that into LINQ with EF Core.

The 2 strings should be string parameters for the query.

What I have tried so far:

  1. lambda-expression like .Where(s => s.someField >= lowerBoundary). Problem: The compiler does not like that at all. enter image description here
  2. Use String.Compare like this .Where(p => string.Compare(p.someField, searchCriteria.lowerBoundary) >= 0 && string.Compare(p.someField, searchCriteria.upperBoundary) <= 0); ** Update (edit): This DOES work**. For some reason it did not in my first try when it was mixed up with other parts of my more complex query.

I have used EF Core 3.1.1

Any ideas?

  • I think you should use either == or the contains for comparison. – Suraj Kumar Feb 13 '20 at 11:59
  • The SQL itself is at least slightly dubious -- keep in mind that `A 1000` is "between" `A 100` and `A 150` as well. Make sure it keeps working "fine" even if your data should change. – Jeroen Mostert Feb 13 '20 at 12:02
  • @JeroenMostert Yes, I am aware of the fact that I am abusing strings for something that looks like a number. ;-) But thank you for pointing that out. – Daniel Hillebrand Feb 13 '20 at 12:22
  • @SurajKumar Neither of those operators would solve my issue. I want to get a range back based on a lexical comparison. – Daniel Hillebrand Feb 13 '20 at 12:25
  • @Nick.McDermaid It did. But I was wrong in my question as I just found out. My second try with "String.Compare" did work when I implemented it in isolation from the other parts of my (more complex) query. I will have to edit my question. Thanks! – Daniel Hillebrand Feb 13 '20 at 12:47

0 Answers0