Optimize sequence of if

Asked

Viewed 57 times

2

I have the following structure and would like to optimize the code of the if, there are already several equal parts. So I made a foreach. But the variable of type Expandoobject, I do not know how to deal with it in a generic way to receive the value of the function.

If someone can and knows how to help me, thank you. Follow below the previous code and my attempt:

//Antes
dynamic line = new ExpandoObject();
line.Filial = ""                
line.Cidade = "";
line.Cep = "";
line.Uf = "";
line.Cliente = "";              
line.Cnpj = "";

if (columnIndexes.Contains (Filial))
  line.Filial = GetValue(table, columnIndexes, Filial, i);

if (columnIndexes.Contains (Cidade))
  line.VendasQuantidade = GetValue(table, columnIndexes, Cidade, i);

if (columnIndexes.Contains (Cep))
  line.Cep = GetValue(table, columnIndexes, Cep, i);

if (columnIndexes.Contains (Uf))
  line.Uf = GetValue(table, columnIndexes, Uf, i);

if (columnIndexes.Contains (Cliente))
  line.Cliente = GetValue(table, columnIndexes, Cliente, i);

if (columnIndexes.Contains (Cnpj))
  line.Cnpj = GetValue(table, columnIndexes, Cnpj, i);

//Depois
var values = new List<string>() {Cidade, Cep, Uf, Cliente, Cnpj, Filial };  

foreach (var value in values)
{
  if (columnIndexes.ContainsValue(value))
  {
    lineToInsert[value] = GetMapValue(table, columnIndexes, value, i);(não funciona)
    lineToInsert[value.ToString] = GetMapValue(table, columnIndexes, value, i);(não funciona)
  }
}

1 answer

1


If I understand correctly you want to check if a dynamic property of a ExpandoObject and set a value is this?

Adapting your code would be something like this:

dynamic line = new ExpandoObject();
        line.Filial = "";             
        line.Cidade = "";
        line.Cep = "";
        line.Uf = "";
        line.Cliente = "";              
        line.Cnpj = "";

        var fields = new List<string>() { "Cidade", "Cep", "Uf", "Cliente", "Cnpj", "Filial", "Algum" };
        // dicionário com as propriedades do ExpandoObject
        var dicLine = (IDictionary<string, object>)line;

        foreach(var f in fields)
        {
            if (dicLine.ContainsKey(f))
            {
                dicLine[f] = "teste";
            }
        }

        Console.WriteLine("line.Cep=" + line.Cep);

Just change the value "test" by the value you have to set in your code.

I put a functional example here: https://dotnetfiddle.net/OatFbt

  • Thank you very much! That was exactly what was missing convert into dictionary. And this is because before he was an object and I was trying to work with him like a dictionary, right?

  • Yes, in the background as it is a "typed" object, it cannot be like a class, so a ExpandoObject works with a dictionary, and to "take" its properties by name has to make the conversion to dictionary, only access by direct key does not work, apparently what was happening in this line: lineToInsert[value]

  • I get it, thank you very much!

  • Then don’t forget to accept the answer if it helped

Browser other questions tagged

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