Click here to Skip to main content
15,891,184 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I need to loop through the columns 'activity', 'mat_or' and 'mat_med' on a datatable that has duplicate values ​​and check which of these columns have the largest number of different records referring to 'num_servico'. See an example of how is my datatable:

contrato|servico|medicao|atividade|quant_at|mat_or| quant_or|mat_med|quant_med

4100003268OP| 14040001253| 1526| 227544| 2| 0252-0017-8| 74| 0060-0004-5| 404 
4100003268OP| 14040001253| 1526| 227544| 2| 0252-0017-8| 74| 0219-0028-0| 0,653
4100003268OP| 14040001253| 1526| 227544| 2| 0252-0017-8| 74| 0223-0010-4| 34
4100003268OP| 14040001253| 1526| 227544| 2| 0252-0017-8| 74| 0252-0017-8| 74
4100003268OP| 14040001253| 1526| 220515| 68| 0252-0017-8| 74| 0060-0004-5| 404 
4100003268OP| 14040001253| 1526| 220515| 68| 0252-0017-8| 74| 0219-0028-0| 0,653 
4100003268OP| 14040001253| 1526| 220515| 68| 0252-0017-8| 74| 0223-0010-4| 34 
4100003268OP| 14040001253| 1526| 220515| 68| 0252-0017-8| 74| 0252-0017-8| 74 
4100003268OP| 14040001253| 1526| 225011| 404| 0252-0017-8| 74| 0060-0004-5| 404 
4100003268OP| 14040001253| 1526| 225011| 404| 0252-0017-8| 74| 0219-0028-0| 0,653 
4100003268OP| 14040001253| 1526| 225011| 404| 0252-0017-8| 74| 0223-0010-4| 34 
4100003268OP| 14040001253| 1526| 225011| 404| 0252-0017-8| 74| 0252-0017-8| 74 

That done, I need to add data from other columns in a datatable where the columns 'contract', 'service' and 'measurement' is repeated in accordance with the highest amount obtained in columns covered. See how the datatable needs to stay:

4100003268OP| 14040001253| 1526| 227544| 2| 0252-0017-8| 74| 0060-0004-5| 404 
4100003268OP| 14040001253| 1526| 220515| 68| | | 0219-0028-0| 0,653
4100003268OP| 14040001253| 1526| 225011| 404| | | 0223-0010-4| 34
4100003268OP| 14040001253| 1526| | | | | 0252-0017-8| 74


With that, I ask the help to do data manipulation for I know not how to manipulate a datatable.

My datatable is being generated through a linq query, see the query:

C#
var ativ = (from Servico in servico.AsEnumerable()
                        join Medicao in medicao.AsEnumerable() on new { num_contrato = Servico["num_contrato"], num_ordem = Servico["num_servico"] } equals new { num_contrato = Medicao["num_contrato"], num_ordem = Medicao["num_servico"] }
                        join MOrcado in material_orcado.AsEnumerable() on new { num_contrato = Servico["num_contrato"], num_servico = Servico["num_servico"] } equals new { num_contrato = MOrcado["num_contrato"], num_servico = MOrcado["num_servico"] } into MaterialOrcado
                        from MO in MaterialOrcado.DefaultIfEmpty()
                        join MMedido in material_medido.AsEnumerable() on new { num_contrato = Servico["num_contrato"], num_servico = Servico["num_servico"] } equals new { num_contrato = MMedido["num_contrato"], num_servico = MMedido["num_servico"] } into MaterialMedido
                        from MM in MaterialMedido.DefaultIfEmpty()
                              group new { Servico, Medicao, MO, MM} by new
                              {
                                  num_contrato = Servico["num_contrato"],
                                  num_servico = Servico["num_servico"],
                                  medicao_num = Medicao["medicao_num"] == DBNull.Value ? null : Medicao["medicao_num"].ToString(),
                                  cod_atividade = Medicao["atividade"] == DBNull.Value ? null : Medicao["atividade"].ToString(),
                                  material_orcado = (MO == null) ? "" : MO["material_orcado"].ToString(),
                                  quantidade_orcado = (MO == null) ? "" : MO["quant_material_orcado"].ToString(),
                                  material_medido = (MM == null) ? "" : MM["material_medido"].ToString(),
                                  quantidade_medido = (MM == null) ? "" : MM["quant_material_medido"].ToString()
                              } into AMOM
                              select new
                              {
                                  num_contrato = AMOM.Key.num_contrato,
                                  num_servico = AMOM.Key.num_servico,
                                  medicao_num = AMOM.Key.medicao_num,
                                  cod_atividade = AMOM.Key.cod_atividade,
                                  atividade_quant_medida = AMOM.Sum(valor => Convert.ToInt32(valor.Medicao["quantidade"])),
                                  material_orcado = AMOM.Key.material_orcado,
                                  quantidade_orcado = AMOM.Key.quantidade_orcado,
                                  material_medido = AMOM.Key.material_medido,
                                  quantidade_medido = AMOM.Key.quantidade_medido
                              }).Distinct();


        DataTable dtAtiv = ativ.ToDataTable();
        dtAtiv.TableName = "Atividade";
Posted
Updated 14-Jan-15 12:05pm
v2
Comments
Sinisa Hajnal 15-Jan-15 2:15am    
I'd say leave out linq and use stored procedure. Databases are good with aggregating large amounts of data. :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900