c# – Simplificar LINQ p => p.Tipo.ToUpper().Equals(“S”) && p.Modo.ToUpper().Equals(“S”)…,n

Question:

How to simplify LINQ expression?

p => p.Tipo.ToUpper().Equals("S") && p.Modo.ToUpper().Equals("S")...,n

Answer:

You will probably wonder if this is simpler. But it is, despite being bigger. And it is the most correct way, although I have doubts if the most correct one should be used.

p => p.Tipo.Equals("S", StringComparison.InvariantCultureIgnoreCase) &&
     p.Modo.Equals("S", StringComparison.InvariantCultureIgnoreCase)...,n

Unless you have a more specific context, it can't do better than that.

If you prefer the "wrong" way:

p => p.Tipo.ToUpper() == "S" && p.Modo.ToUpper() == "S"...,n

I put it on GitHub for future reference .

Other than that, if the condition is always the same, you can make a helper method that goes through all the necessary members and reduce the code a little, if it has many repeated conditions. The list of members can be passed manually or can be further reduced using reflection. But in this case it will be confusing, not very performative and if it is not to be done with all the members, I would have to use annotations. In other words, something so complicated that it would hardly justify its use.

And then we are talking about making code reduced and not simplified.

Scroll to Top