Search range of numbers (string type) with Expression Lambda?

Asked

Viewed 173 times

7

I have this data model and I don’t know how to do the instruction lambda:

public class CidadeAbrangencia
    {
        [Key]
        public int CidadeAbrangencia_Id { get; set; }
        public string CEPInicial { get; set; }
        public string CEPFinal  { get; set; }
    }

Example, I need to search all Zip Codes that start at 03500-000 up to 04000-000.

Consultation:

IEnumerable<CidadeAbrangencia> retAbrangencia = context.CidadeAbrangencias.Where(x => x.CEPInicial >= '03500-000' && '04000-000');

My problem is that the CEP Inicial and the ZIP CODE are guys string, then how to do?

Being String gives error:

The >= operator cannot be applied to string and sgtring type operands

inserir a descrição da imagem aqui

2 answers

7

If everything is formatted correctly I could use this syntax, if I understood what you want would be something like this:

x => String.Compare(x.CEPInicial, "03500-000") >= 0 && String.Compare(x.CEPFinal, "04000-000") <= 0

Another way that should work:

x => x.CEPInicial.CompareTo("03500-000") >= 0 && x.CEPFinal.CompareTo("04000-000") <= 0

I put in the Github for future reference.

These methods make the comparison and return:

  • -1 if left is less than right
  • 0 if the values are equal
  • 1 if the value on the left is greater than the value on the right

Documentation of CompareTo() and Compare().

It only depends on actually having all the digits correctly placed. If it is not so it becomes extremely complicated.

When the string has the first characters larger than the following the order can be established exactly as if made with number.

However, if you are using this with Entity Framework, and the question does not say about it, but gives clues at one point, and gives clues that not at another (IEnumerable<CidadeAbrangencia>), can generate a very confusing SQL query or have problems. But it can be good because it depends on the preview be very well done to understand these things.

I do not guarantee that the result will be correct because even the question is not clear what would be the logic of this, but at least it would work and solve the reported problem.

  • But data type error occurs: The >= operator cannot be applied to strings and sgtring operands

  • I edit the post and added the error message

  • Huh? There’s the operator >= for string and string?

  • I think that operator >= does not work for String.

  • Exactly @gato ! does not work ! And how can I get around this problem ?

  • @LINQ Oops, wrong language :D

  • @adrianojc Ou vc converts using the TryParse (I don’t think it’s a good idea) or you will have to elaborate an extension method. But there must be something native to this, you have to check the documentation first.

  • @Maniero I even went to look in C# 8 to see if there was something =D It’s probably Kotlin

Show 3 more comments

6


The operator >= does not work on strings in lambda expression, maybe in the future this will change.

To get this behavior you must use the method Comparet, I believe I’m the closest to this. I did it based on that reply soen.

See the code:

public class Program {
    public static void Main() {             
        var cidades = new List<CidadeAbrangencia> {
            new CidadeAbrangencia {
                CidadeAbrangencia_Id = 1,
                CEPInicial = "03500-001",
                CEPFinal = "04000-000"
            },
            new CidadeAbrangencia {
                CidadeAbrangencia_Id = 2,
                CEPInicial = "03500-002",
                CEPFinal = "03211-000"
            },
            new CidadeAbrangencia {
                CidadeAbrangencia_Id = 3,
                CEPInicial = "03500-005",
                CEPFinal = "05800-000"
            },
            new CidadeAbrangencia {
                CidadeAbrangencia_Id = 4,
                CEPInicial = "09211-011",
                CEPFinal = "99900-999"
            },
            new CidadeAbrangencia {
                CidadeAbrangencia_Id = 5,
                CEPInicial = "03500-021",
                CEPFinal = "04000-000"
            }
        };      
        var res = cidades.Where(x => x.CEPInicial.CompareTo("03500-000") >= 0 && x.CEPFinal.CompareTo("04000-000") <= 0).ToList();
        res.ForEach(c => WriteLine(c.CidadeAbrangencia_Id));        
    }
}

Exit

1
2
5

See working on .NET Fiddle.

I suggest you adapt this code and use it carefully because I haven’t done all the tests.

  • I will implement and test from there put here the result thank you for while.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.