Fill Combobox with Dapper

Asked

Viewed 283 times

5

I’m using a component of combobox with the Dapper, but I’m not able to fill the combobox properly, follow code excerpt:

LookUpEdit ctlControle = (LookUpEdit) pr_Controle;
var parametro = new DynamicParameters();
parametro.Add("@TABELA_NOME", pr_TabelaNome);
parametro.Add("@TABELA_CAMPO_VALOR", pr_CampoValor);
parametro.Add("@TABELA_CAMPO_DISPLAY", pr_CampoDisplay);

ctlControle.Properties.DataSource = acessoDados.criarConexao().Query("PRP_PREENCHER_COMBO", parametro, commandType: CommandType.StoredProcedure);
ctlControle.Properties.ValueMember = pr_CampoValor;
ctlControle.Properties.DisplayMember = pr_CampoDisplay;
ctlControle.Properties.PopulateColumns();

So when adding to datasource it wrongly fills by placing all fields on the same line of Combobox (lookupedit of devexpress). How can I do this?

1 answer

3


One thing Dapper doesn’t clarify is that his return isn’t exactly structured the way a data source of a component . NET waits, so I wrote an extension method that converts the result to something more intelligible, like a list, for example:

public static class DapperExtensions
{
    public static IEnumerable<T> ToTypedList<T>(this IEnumerable<dynamic> list, bool colunasMaiusculas = true)
        where T: class, new()
    {
        var properties = typeof(T).GetProperties();

        foreach (var element in list)
        {
            var obj = new T();
            foreach (var keyValue in ((IDictionary<string, object>) element).Where(e => e.Value != null)) 
            {
                PropertyInfo property;
                if (colunasMaiusculas) 
                { 
                    property = properties.FirstOrDefault(p => p.Name.ToUpper() == keyValue.Key);
                } else {
                    property = properties.FirstOrDefault(p => p.Name == keyValue.Key);
                }

                switch (property.PropertyType.ToString())
                {
                    case "System.Int32":
                        property.SetValue(obj, Convert.ToInt32(keyValue.Value));

                        break;
                    case "System.Int64":
                        property.SetValue(obj, Convert.ToInt64(keyValue.Value));

                        break;
                    case "System.DateTime":
                        property.SetValue(obj, Convert.ToDateTime(keyValue.Value));

                        break;
                    default:
                        if (keyValue.Value != null)
                        {
                            property.SetValue(obj, keyValue.Value);
                        }

                        break;
                }
            }

            yield return obj;
        }
    }
}

Use like this:

ctlControle.Properties.DataSource = acessoDados.criarConexao().Query("PRP_PREENCHER_COMBO", parametro, commandType: CommandType.StoredProcedure).ToTypedList<ClasseDeDados>().ToList();

ClasseDeDados is a class that has the same fields that come from the bank.

  • 1

    is a very interesting solution, there is no way to do this without putting a Classededados ? because I’m doing it in a generic function and I couldn’t get a way to pass a Classededados as parameter.

  • another issue, worth thinking of using Dapper or better stay in the EF itself ?

  • You can do with ExpandoObject, but I’m not sure if it works. About Dapper or EF, it depends on what you want. Dapper gives a little more work but the performance gets animal. Already the EF solves everything for you. You only care about the business rule of your system, but it is obviously slower.

  • 1

    I got it, thanks for your help, I owe you a bill =]

Browser other questions tagged

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