Manipulando DataTable (Ajuda)
09/09/2014
0
[img:descricao=Exemplo_Soma]http://arquivo.devmedia.com.br/forum/imagem/302406-20140909-155438.jpg[/img]
Como podem observar na imagem(garrancho, rs), preciso somar também a quantidade de uma mesma atividade executada em cada medição. A repetição de registro será de acordo com a diferença dos códigos das atividades.
Se possível poderiam me ajudar por favor? Agradecido.
Renato Pereira
Posts
09/09/2014
Joel Rodrigues
09/09/2014
Renato Pereira
DataTable servico = ds.Tables[0]; DataTable ata = ds.Tables[1]; DataTable medicao = ds.Tables[2]; var results = (from Servico in servico.AsEnumerable() join Ata in ata.AsEnumerable() on new { num_contrato = Servico["num_contrato"].ToString(), num_ordem = Servico["num_servico"].ToString() } equals new { num_contrato = Ata["num_contrato"].ToString(), num_ordem = Ata["num_servico"].ToString() } join Medicao in medicao.AsEnumerable() on new { num_contrato = Servico["num_contrato"].ToString(), num_ordem = Servico["num_servico"].ToString() } equals new { num_contrato = Medicao["num_contrato"].ToString(), num_ordem = Medicao["num_servico"].ToString() } group new { Servico, Ata, Medicao } by new { num_contrato = Servico["num_contrato"].ToString(), num_servico = Servico["num_servico"].ToString(), escritorio = Servico["escritorio"] == DBNull.Value ? null : Servico["escritorio"].ToString(), area_telefonica = Servico["area_telefonica"] == DBNull.Value ? null : Servico["area_telefonica"].ToString(), servico_tipo = Servico["servico_tipo"] == DBNull.Value ? null : Servico["servico_tipo"].ToString(), servico_subtipo = Servico["servico_subtipo"] == DBNull.Value ? null : Servico["servico_subtipo"].ToString(), data_geracao = Servico["data_geracao"] == DBNull.Value ? null : Convert.ToDateTime(Servico["data_geracao"]).ToString("dd/MM/yyyy"), ordem_interna = Servico["ordem_interna"] == DBNull.Value ? null : Servico["ordem_interna"].ToString(), boletim_anormalidade = Servico["boletim_anormalidade"] == DBNull.Value ? null : Servico["boletim_anormalidade"].ToString(), plano_trabalho = Servico["plano_trabalho"] == DBNull.Value ? null : Servico["plano_trabalho"].ToString(), aep_num = Ata["ata_estudo_num"] == DBNull.Value ? null : Ata["ata_estudo_num"].ToString(), aep_inicio = Ata["ata_estudo_inicio"] == DBNull.Value ? null : Convert.ToDateTime(Ata["ata_estudo_inicio"]).ToString("dd/MM/yyyy"), aep_fim = Ata["ata_estudo_fim"] == DBNull.Value ? null : Convert.ToDateTime(Ata["ata_estudo_fim"]).ToString("dd/MM/yyyy"), aep_comeco = Ata["ata_estudo_comeco"] == DBNull.Value ? null : Convert.ToDateTime(Ata["ata_estudo_comeco"]).ToString("dd/MM/yyyy"), aep_conclusao = Ata["ata_estudo_conclusao"] == DBNull.Value ? null : Convert.ToDateTime(Ata["ata_estudo_conclusao"]).ToString("dd/MM/yyyy"), medicao_num = Medicao["medicao_num"] == DBNull.Value ? null : Medicao["medicao_num"].ToString(), medicao_data_criacao = Medicao["medicao_data_criacao"] == DBNull.Value ? null : Convert.ToDateTime(Medicao["medicao_data_criacao"]).ToString("dd/MM/yyyy"), medicao_acesso_atividade = Medicao["medicao_acesso_atividade"] == DBNull.Value ? null : Medicao["medicao_acesso_atividade"].ToString(), medicao_data_classificacao = Medicao["medicao_data_classificacao"] == DBNull.Value ? null : Convert.ToDateTime(Medicao["medicao_data_classificacao"]).ToString("dd/MM/yyyy"), cod_atividade = Medicao["atividade"] == DBNull.Value ? null : Medicao["atividade"].ToString() } into GM select new { num_contrato = GM.Key.num_contrato, num_servico = GM.Key.num_servico, escritorio = GM.Key.escritorio == null ? null : GM.Key.escritorio, area_telefonica = GM.Key.area_telefonica == null ? null : GM.Key.area_telefonica, serivo_tipo = GM.Key.servico_tipo == null ? null : GM.Key.servico_tipo, serivo_subtipo = GM.Key.servico_subtipo == null ? null : GM.Key.servico_subtipo, data_geracao = GM.Key.data_geracao == null ? null : GM.Key.data_geracao, ordem_interna = GM.Key.ordem_interna == null ? null : GM.Key.ordem_interna, boletim_anormalidade = GM.Key.boletim_anormalidade == null ? null : GM.Key.boletim_anormalidade, plano_trabalho = GM.Key.plano_trabalho == null ? null : GM.Key.plano_trabalho, aep_num = GM.Key.aep_num == null ? null : GM.Key.aep_num, aep_inicio = GM.Key.aep_inicio == null ? null : GM.Key.aep_inicio, aep_fim = GM.Key.aep_fim == null ? null : GM.Key.aep_fim, aep_comeco = GM.Key.aep_comeco == null ? null : GM.Key.aep_comeco, aep_conclusao = GM.Key.aep_conclusao == null ? null : GM.Key.aep_conclusao, medicao_num = GM.Key.medicao_num == null ? null : GM.Key.medicao_num, medicao_data_criacao = GM.Key.medicao_data_criacao == null ? null : GM.Key.medicao_data_criacao, medicao_valor = GM.Sum(valor => Convert.ToDecimal(valor.Medicao["medicao_valor_pagar"])).ToString("###,##0.00") == null ? null : GM.Sum(valor => Convert.ToDecimal(valor.Medicao["medicao_valor_pagar"])).ToString("###,##0.00"), medicao_acesso_atividade = GM.Key.medicao_acesso_atividade == null ? null : GM.Key.medicao_acesso_atividade, medicao_data_classificacao = GM.Key.medicao_data_classificacao == null ? null : GM.Key.medicao_data_classificacao, cod_atividade = GM.Key.cod_atividade == null ? null : GM.Key.cod_atividade, atividade_quant_medida = GM.Sum(quantidade => Convert.ToInt32(quantidade.Medicao["quantidade"])).ToString() == null ? null : GM.Sum(quantidade => Convert.ToInt32(quantidade.Medicao["quantidade"])).ToString() } ).Distinct();
18/09/2014
Renato Pereira
contrato | servico | Medição | 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 | 0653
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 | 0653
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 | 0653
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
Feito isso, eu preciso adicionar os dados em um outro datatable, onde as colunas de 'contrato', 'serviço' e 'medição' é repetida de acordo com a quantidade obtida nas colunas percorridas. Veja como preciso que o DataTable fique:
contrato | servico | Medição | 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 | 220515 | 68 | | | 0219-0028-0 | 0653
4100003268OP | 14040001253 | 1526 | 225011 | 404 | | | 0223-0010-4 | 34
4100003268OP | 14040001253 | 1526 | | | | | 0252-0017-8 | 74
Observem que foi repetido as colunas de acordo com a coluna 'mat_met' que resultou em um maior número de registros diferentes.
Com isso, peço a ajuda para fazer essa manipulação de dados.
Meu datatable está sendo gerado através de uma consulta LINQ, veja a consulta:
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" ;
Clique aqui para fazer login e interagir na Comunidade :)