Dynamic linq with C#

Asked

Viewed 234 times

3

Inside the new in Linq. I need to create the dynamic properties based on a data table how can I do this?

var query = (from indicador in tableIndicado.AsEnumerable()
    group indicador by indicador.Field<string>("IND_CODUSU") into g
    select new {

      #region Privot Table mensal

      // Janeiro
      Jan_Valor = g.Where(x => x.Field<DateTime>("MTA_DTINIANO").Month == 1).Sum(x => x.Field<decimal?>("MTA_INDVAL")),
      Jan_Meta1 = g.Where(x => x.Field<DateTime>("MTA_DTINIANO").Month == 1).Sum(x => x.Field<decimal?>("MTA_META1")),
      Jan_Meta2 = g.Where(x => x.Field<DateTime>("MTA_DTINIANO").Month == 1).Sum(x => x.Field<decimal?>("MTA_META2")),

      // Fevereiro
      Fev_Valor = g.Where(x => x.Field<DateTime>("MTA_DTINIANO").Month == 2).Sum(x => x.Field<decimal?>("MTA_INDVAL")),
      Fev_Meta1 = g.Where(x => x.Field<DateTime>("MTA_DTINIANO").Month == 2).Sum(x => x.Field<decimal?>("MTA_META1")),
      Fev_Meta2 = g.Where(x => x.Field<DateTime>("MTA_DTINIANO").Month == 2).Sum(x => x.Field<decimal?>("MTA_META2")),

      // Março
      Mar_Valor = g.Where(x => x.Field<DateTime>("MTA_DTINIANO").Month == 3).Sum(x => x.Field<decimal?>("MTA_INDVAL")),
      Mar_Meta1 = g.Where(x => x.Field<DateTime>("MTA_DTINIANO").Month == 3).Sum(x => x.Field<decimal?>("MTA_META1")),
      Mar_Meta2 = g.Where(x => x.Field<DateTime>("MTA_DTINIANO").Month == 3).Sum(x => x.Field<decimal?>("MTA_META2")),

      // Abril
      Abr_Valor = g.Where(x => x.Field<DateTime>("MTA_DTINIANO").Month == 4).Sum(x => x.Field<decimal?>("MTA_INDVAL")),
      Abr_Meta1 = g.Where(x => x.Field<DateTime>("MTA_DTINIANO").Month == 4).Sum(x => x.Field<decimal?>("MTA_META1")),
      Abr_Meta2 = g.Where(x => x.Field<DateTime>("MTA_DTINIANO").Month == 4).Sum(x => x.Field<decimal?>("MTA_META2")),

      // Maio
      Mai_Valor = g.Where(x => x.Field<DateTime>("MTA_DTINIANO").Month == 5).Sum(x => x.Field<decimal?>("MTA_INDVAL")),
      Mai_Meta1 = g.Where(x => x.Field<DateTime>("MTA_DTINIANO").Month == 5).Sum(x => x.Field<decimal?>("MTA_META1")),
      Mai_Meta2 = g.Where(x => x.Field<DateTime>("MTA_DTINIANO").Month == 5).Sum(x => x.Field<decimal?>("MTA_META2")),

      // Junho
      Jun_Valor = g.Where(x => x.Field<DateTime>("MTA_DTINIANO").Month == 6).Sum(x => x.Field<decimal?>("MTA_INDVAL")),
      Jun_Meta1 = g.Where(x => x.Field<DateTime>("MTA_DTINIANO").Month == 6).Sum(x => x.Field<decimal?>("MTA_META1")),
      Jun_Meta2 = g.Where(x => x.Field<DateTime>("MTA_DTINIANO").Month == 6).Sum(x => x.Field<decimal?>("MTA_META2")),

      // Julho
      Jul_Valor = g.Where(x => x.Field<DateTime>("MTA_DTINIANO").Month == 7).Sum(x => x.Field<decimal?>("MTA_INDVAL")),
      Jul_Meta1 = g.Where(x => x.Field<DateTime>("MTA_DTINIANO").Month == 7).Sum(x => x.Field<decimal?>("MTA_META1")),
      Jul_Meta2 = g.Where(x => x.Field<DateTime>("MTA_DTINIANO").Month == 7).Sum(x => x.Field<decimal?>("MTA_META2")),

      // Agosto
      Ago_Valor = g.Where(x => x.Field<DateTime>("MTA_DTINIANO").Month == 8).Sum(x => x.Field<decimal?>("MTA_INDVAL")),
      Ago_Meta1 = g.Where(x => x.Field<DateTime>("MTA_DTINIANO").Month == 8).Sum(x => x.Field<decimal?>("MTA_META1")),
      Ago_Meta2 = g.Where(x => x.Field<DateTime>("MTA_DTINIANO").Month == 8).Sum(x => x.Field<decimal?>("MTA_META2")),

      // Setembro
      Set_Valor = g.Where(x => x.Field<DateTime>("MTA_DTINIANO").Month == 9).Sum(x => x.Field<decimal?>("MTA_INDVAL")),
      Set_Meta1 = g.Where(x => x.Field<DateTime>("MTA_DTINIANO").Month == 9).Sum(x => x.Field<decimal?>("MTA_META1")),
      Set_Meta2 = g.Where(x => x.Field<DateTime>("MTA_DTINIANO").Month == 9).Sum(x => x.Field<decimal?>("MTA_META2")),

      // Outubro
      Out_Valor = g.Where(x => x.Field<DateTime>("MTA_DTINIANO").Month == 10).Sum(x => x.Field<decimal?>("MTA_INDVAL")),
      Out_Meta1 = g.Where(x => x.Field<DateTime>("MTA_DTINIANO").Month == 10).Sum(x => x.Field<decimal?>("MTA_META1")),
      Out_Meta2 = g.Where(x => x.Field<DateTime>("MTA_DTINIANO").Month == 10).Sum(x => x.Field<decimal?>("MTA_META2")),

      // Novembro
      Nov_Valor = g.Where(x => x.Field<DateTime>("MTA_DTINIANO").Month == 11).Sum(x => x.Field<decimal?>("MTA_INDVAL")),
      Nov_Meta1 = g.Where(x => x.Field<DateTime>("MTA_DTINIANO").Month == 11).Sum(x => x.Field<decimal?>("MTA_META1")),
      Nov_Meta2 = g.Where(x => x.Field<DateTime>("MTA_DTINIANO").Month == 11).Sum(x => x.Field<decimal?>("MTA_META2")),

      // Dezembro
      Dez_Valor = g.Where(x => x.Field<DateTime>("MTA_DTINIANO").Month == 12).Sum(x => x.Field<decimal?>("MTA_INDVAL")),
      Dez_Meta1 = g.Where(x => x.Field<DateTime>("MTA_DTINIANO").Month == 12).Sum(x => x.Field<decimal?>("MTA_META1")),
      Dez_Meta2 = g.Where(x => x.Field<DateTime>("MTA_DTINIANO").Month == 12).Sum(x => x.Field<decimal?>("MTA_META2")),

      #endregion
  });

return dataset; 
  • You want to create the query properties based on the column name (of the dataset), this?

  • That. I have to create them dynamically as it comes from my datatable

  • I don’t think it’s possible, ideally you would use a data structure that doesn’t have a fixed size, so you would add the items in it dynamically, it could be a List or a Dictionary for example.

  • You can give me an example of how to add with List or Dictionary

1 answer

1

First of all I started by expressing better the intention of my code, making a grouping of sums per month:

var query = g.GroupBy(x => x.Field<DateTime>("MTA_DTINIANO").Month, 
   group => group, 
   (month, group) => new {
       Valor = group.Sum(x => x.Field<decimal?>("MTA_INDVAL"),
       Meta1 = group.Sum(x => x.Field<decimal?>("MTA_META1"),
       Meta2 = group.Sum(x => x.Field<decimal?>("MTA_META2")   
   }
);

And instead of having 36 fields I put the query result in a dictionary that I could index per month

query.ToDictionary(x => x.Key);
  • I understood what you did only that the problem is that the columns of the data table are mounted dynamically so I can have different columns when assembling the group

  • @I think you’re gonna have to give an example. What I can add is that it may make sense to add a column that is always present so that it can be grouped according to its value.

Browser other questions tagged

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