Alternative to IF and Else in a specific problem

Asked

Viewed 93 times

2

PROBLEM

I have stored in the database five types of data sets, different from each other. Imagine, to illustrate, that this data set is details of five different fruits, for example, bananas, apples, oranges, grapes and pears, for example. The data structure is different for each of the fruits, in this hypothetical example.

In the application, the user enters as the appropriate search term and returns the details of all five fruits; or, only of one; or, two; or, three; or four; or none. For example, if the user enters as the term "hard bark" will match the feature present in the five fruits.

I use a control variable for each result. Each variable can contain "record" or no "record". So, since there are five variables, I have thirty-two possibilities to check.

I use Listview to display the data in the application, and to improve the user experience, set a control for each fruit, because the structure of the data tables are different for each fruit.

I use If and Else as a solution, and it works.

I aim to solve this problem using a different approach in c#, without having to modify the structure in the bank, since there are thousands of records already consolidated.

A clipping of how I’m doing:

//Situação 1 - V V V V V

 if (detalhes1 != 0 && detalhes2 == 0 && detalhes3 == 0 && detalhes4 == 0 && 
 detalhes5 == 0) 

 {

 ....
 .
 .

  ListViewDetalhes1.DataSource = detalhes1Colecao;                                 
  ListViewDetalhes1.DataBind();

  }

and so I will map each possibility until "close" the thirty-two.

  • 1

    I don’t quite understand your question, but are the details of the fruit right? Where is the variable searched term in question, where you make the comparison of that searched term?

  • 1

    I think it might be interesting you apply Azure Machine Learning in this case, to avoid having to use IF. https://www.bing.com/search?q=azure+machine+learning

  • @Alberttsantos in the business layer I have a method where I pass the string that the user inserts to search. For example: details1Collection = details1Negocios.Consultatodas(thermoresearched); I do this with all 5 variables, because the data model is different in the tables. After, I take the result of each variable...

  • Is it me or are you assigning a responsibility to the application that would actually be from the database? Because if you are behind the data, why "filter" the received data again?

  • It is necessary to meet the searched criteria, and display the appropriate result.

  • Show the database structure please, and some sample data

Show 1 more comment

1 answer

3

Why don’t you use a set of boolean "flags" for each of your thirty-two variables and then submit to a "SWITCH", it would be something +/- like this (requires adjustments):

class Program
{
    static void Main()
    {
        bool[] array = new bool[32];
        int[] detalhes = new int[32];
        string[] frutas = new string[5];
        frutas[0] = "banana";
        frutas[1] = "maçã";
        frutas[2] = "laranja";
        frutas[3] = "uva";
        frutas[4] = "pera";


        for (int i = 0; i < frutas.Length - 1; i++)
        {
            for (int y = 0; y < detalhes.Length; y++)
            {
                array[i] = false;

                switch (detalhes[i])
                {
                    case 0:
                        {
                            array[i] = true;
                            break;
                        }
                    case 1:
                        {
                            continue;
                        }
                    default:
                        break;
                }
            }
        }

        Console.ReadKey();
    }
}

Browser other questions tagged

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