5
I have my Model account as below:
public class Conta
{
public int Id {get;set;}
public int ClienteId {get;set;}
public Cliente Cliente {get;set;}
public int ContaBancariaId {get;set;}
public ContaBancaria ContaBancaria {get;set;}
public int PlanoId {get;set;}
public Plano Plano {get;set;}
public string Descricao {get;set;}
public DateTime Data {get;set;}
public decimal Valor {get;set;}
}
For my filter and result I have the following models:
public class ContaBusca
{
public AgruparPor AgruparPor {get;set;}
}
public enum AgruparPor
{
Cliente,
ContaBancaria,
Plano
}
And here’s my comeback:
public class ContaResultadoBusca
{
public string Nome {get;set;} //Pode ser o nome do Cliente, Conta Bancaria ou Plano
public List<ContaResultadoBuscaItem> Contas {get;set;}
}
public class ContaResultadoBuscaItem
{
public string Descricao {get;set;}
public DateTime Data {get;set;}
public decimal Valor {get;set;}
}
And in my service I receive the parameter and return as follows
public class ContaServico
{
public ContaResultadoBusca AgruparPor(AgruparPor agrupador)
{
if (agrupador == AgruparPor.Cliente)
{
return dbo.Contas.GroupBy(conta => conta.Cliente).Select(conta => new ContaResultadoBusca { Nome = conta.Nome })..
}
if(agrupador == AgruparPor.ContaBancaria)
{
return dbo.Contas.GroupBy(conta => conta.ContaBancaria).Select(conta => new ContaResultadoBusca { Nome = conta.Nome })..
}
}
}
How can I group according to the past parameter without having to do so many if
?
In the example there are only 2, but could be more...
In this case that you exemplified, I don’t know any other way than using a switch instead of if. What one can also do is to have a method to manufacture the Expression of the grouping, more in any way will have a control structure to identify the grouping
– Julio Borges
@Julioborges I could use ternary operation within groupBy... But if you have too many groupers it gets very bad maintenance and code reading...
– Marcelo Dias
It sure would look very bad, the best even will be to replace the if for a switch.
– Julio Borges
The nice thing about Switch is that if you fail to implement an Enum option, the compiler warns you and makes it easier to avoid errors due to lack of implementation
– Julio Borges
Yeah, but I still have the duplicate of my Select... That’s what I’m not liking... I tried with Func too
– Marcelo Dias
the only way you can do that is if you use polymorphism by having your entity count as abstract and the rest inherit from it. in Account you would have the data as id and name.
– Marco Souza
@Marconciliosouza, good option too
– Julio Borges
If the column matches the Enum string, you can do it with a Linq Query
– Leandro Angelo
@Leandroangelo in particular I do not like syntax of Innumerable query rsrsrs But answer there... If it is the only option...
– Marcelo Dias