0

I am returning a value from SQL. Let's give it a name as ReturnedFromSQL. In my application, I am giving an input. Let's give it a name as Input. I want to compare these two and do an activity. Currently, I am doing this with an IFcondition. Let's say, from SQL I am getting FAB01and as the Input I am getting Fab01. What I want to do is to jump to ELSEpart after comparing these two if these two are not same and execute IF part if these two are same. It is obvious that just the letters have changed but the idea is same. Since these two are same, I need to execute what is in IFpart instead of jumping to ELSE. But it is not happening now. How do I do that? Can anyone help me? Thank you in advance.

IF (ReturnedFromSQL == Input )
{
 return RedirectToAction("Exist");
}
else
{
//Doing Something
}

5 Answers5

2

Assuming that both ReturnedFromSQL and Input are strings. This code will convert both the value to lower for comparison so no issue if any of the string has same character but in different level(upper/lower)

if (ReturnedFromSQL.ToLower().Trim() == Input.ToLower().Trim() )
{
 return RedirectToAction("Exist");
}
else
{
//Doing Something
}
Gaurang Dave
  • 3,956
  • 2
  • 15
  • 34
  • You do not need else. – CodingYoshi Apr 24 '18 at 03:58
  • @CodingYoshi he has taken that part directly from the question. although the code will still work without the else. It still provides some safety. If the OP wishes there is no harm in using `else` – Neville Nazerane Apr 24 '18 at 04:03
  • @nevillenazerane code which is not needed is not needed. Period. Thus, it is harmful-to me. Otherwise you may as well put a loop there as well. And what safety does it provide? – CodingYoshi Apr 24 '18 at 04:06
  • the safety it provides is: when you edit the if a block, you forget the return portion, you have a compile-time warning. These are things that happen. – Neville Nazerane Apr 24 '18 at 04:11
  • why would you want a loop. that is actually not needed. – Neville Nazerane Apr 24 '18 at 04:11
  • @nevillenazerane If you forget the return then thats a bug in your code so what safety did you get? I know the loop is not needed but according to your logic *there is no harm* so it should be fine. The point is when you write code you should only write code that makes sense and code which is needed. The `else` is not needed. It is useless. Useless code has no business being in code. – CodingYoshi Apr 24 '18 at 10:17
  • lol anything wrongly written/forgotten by a coder is a bug. so whats your point? YES any safety is to prevent such "bugs". There are tons of things usually written for safety (mainly maintainability). If a word can save a future bug, whats the harm? Lot of prople use else after return, it is up to the coder. – Neville Nazerane Apr 24 '18 at 20:19
  • adding `{}` for single line statements and using naming arguments when calling functions are some other examples of safety that is not *needed* in the code but is used by many for clarity and safety. – Neville Nazerane Apr 24 '18 at 20:22
2

You can use string.Equals(). The best part is that you do not need to trim or lowercase explicitly.

if(string.Equals(ReturnedFromSQL, Input, StringComparison.CurrentCultureIgnoreCase))
{
  return RedirectToAction("Exist");
}
Ashwini Verma
  • 7,477
  • 6
  • 36
  • 56
2

Take care if you follow the advice in some of the other answers. ToLower() is only safe in English and a handful of other languages. In many languages, there may be multiple upper/lower case representations of the same letter or letter combination, or certain accents may be considered equal or not equal, breaking such logic. There be the dragons.

Try this instead, changing the StringComparison as per your use case:

using System;

public class Program
{
    public static void Main()
    {
        string ReturnedFromSQL = "FAB01";
        string Input = "Fab01";
        if (String.Equals(ReturnedFromSQL, Input, StringComparison.CurrentCultureIgnoreCase))
        {
            Console.WriteLine("Equal");
        }
        else
        {
            Console.WriteLine("Different");
        }
    }
}
Adam G
  • 1,283
  • 1
  • 6
  • 15
1

if both values are string then you can apply ToLower() and Trim() (as in x.Trim().ToLower()) functions on both party in order to have a unified value. but even without that you still should be good, so i guess the problem is something else. i advise debugging the condition and check the value of both sides and see if either one is null or empty.

Siavash Rostami
  • 1,883
  • 4
  • 17
  • 31
1

Your condition can use Equals

if (String.Equals(ReturnedFromSQL, Input, StringComparison.OrdinalIgnoreCase))
Neville Nazerane
  • 6,622
  • 3
  • 46
  • 79