Group a list of products and services
There are some improvemants possible but they are rather of technical nature then about performance as I don't think you can do much about it until you have measured it either with benchmarks or a profiler.




products.Data.AsEnumerable()



This looks like you are working with a DataTable, aren't you? If the data comes from a database then you should group it on the server.




CodigoServico = int.Parse(string.IsNullOrEmpty(x["CodigoServico"].ToString()) ? "0" : x["CodigoServico"].ToString()),
CodigoPeca = int.Parse(string.IsNullOrEmpty(x["CodigoPeca"].ToString()) ? "0" : x["CodigoPeca"].ToString()),
CodigoFamilia = int.Parse(string.IsNullOrEmpty(x["CodigoFamilia"].ToString()) ? "0" : x["CodigoFamilia"].ToString()),



You should create a helper extension or function for such lines, it's pretty ugly to repeat it so many times. Even two times is already too many.




.GroupBy(x =&gt; new { x.CodigoPeca, x.CodigoFamilia, x.Familia, x.Peca })



You are grouping by the same properties as the object has that you create in the next step:


Produto = new Produto
{
  CodigoPeca = x.Key.CodigoPeca,
  CodigoFamilia = x.Key.CodigoFamilia,
  Familia = x.Key.Familia,
  Peca = x.Key.Peca
},



Instead of doing this twice you should implement the IEquatable&lt;Produto&gt; interface on this object and use this instance as a key so that you only have a single new.




Servicos = x.Select(y =&gt; new ProdutoServico
{
  CodigoServico = y.CodigoServico,
  Hash = y.Hash,
  Servico = y.Servico,
  Valor = y.Valor
}



You can replace this with Servicos = x if you use another overload of GroupBy and project each item right away.