What is equivalent to clause between, for comparasion strings in LINQ or lambda expression of?

15,033

Solution 1

Have you tried:

yourDataContext.Country.Where(c => c.Name >= "Argentina" && c.Name <= "Jamaica");

Solution 2

perpetrators >= and <= are not for strings. they will throw compile time error to you. you can use CompareTo for this as shown below

x.Name.CompareTo(Start) >= 0 && x.Name.CompareTo(End) <= 0
Share:
15,033
Mateus Benetti
Author by

Mateus Benetti

I'm responsible for technically leading a team of software developers. Guiding on good development practices, performing code review, aiming to maintain a product with a good life cycle and architecture. I has been to work with web systems developed with .net framework (C# and VB.NET) since 2009.

Updated on June 12, 2022

Comments

  • Mateus Benetti
    Mateus Benetti almost 2 years

    How do I filter a query interval of two string using LINQ or Lambda Expression.

    example:

    SELECT * FROM dbo.Country WHERE Name BETWEEN "Argentina" AND "Jamaica";
    
  • Mateus Benetti
    Mateus Benetti over 11 years
    doing so, it asks me the following error: Operator '<=' can not be applied to operands of type 'string' and 'string'
  • Rawling
    Rawling over 11 years
    You have to use c.Name.CompareTo("Argentina") >= 0 && c.Name.CompareTo("Jamaica") <= 0, but I'm not sure whether this translates through the entity framework.
  • JustLearning
    JustLearning over 4 years
    Works in linq to entities