using System.Collections.Generic;
using System.Runtime.Serialization.Formatters.Binary;
public static void Main()
var lst = new List<order>();
var lstAgrPai = new List<order>();
var lstFilhos = new List<order>();
lst.Add(new order{guid = "aaa",Parent=" ",ProductID="109",Description="Burger" ,Quantity=(decimal)2.00});
lst.Add(new order{guid = "bbb",Parent="aaa",ProductID="800",Description="Bacon" ,Quantity=(decimal)0.70});
lst.Add(new order{guid = "ccc",Parent="xxx",ProductID="101",Description="Fries" ,Quantity=(decimal)1.80});
lst.Add(new order{guid = "ddd",Parent="xxx",ProductID="101",Description="Fries" ,Quantity=(decimal)1.80});
lst.Add(new order{guid = "eee",Parent="xxx",ProductID="101",Description="Fries" ,Quantity=(decimal)1.80});
lst.Add(new order{guid = "fff",Parent=" ",ProductID="102",Description="Large Fries",Quantity=(decimal)2.80});
lst.Add(new order{guid = "ggg",Parent=" ",ProductID="109",Description="Burger" ,Quantity=(decimal)2.00});
lst.Add(new order{guid = "hhh",Parent="ggg",ProductID="800",Description="Bacon" ,Quantity=(decimal)0.70});
lst.Add(new order{guid = "iii",Parent=" ",ProductID="102",Description="Large Fries",Quantity=(decimal)2.80});
lst.Add(new order{guid = "zz1",Parent="" ,ProductID="105",Description="Meat" ,Quantity=(decimal)3.10});
lst.Add(new order{guid = "zz2",Parent="" ,ProductID="105",Description="Meat" ,Quantity=(decimal)3.10});
lst.Add(new order{guid = "zz3",Parent="" ,ProductID="105",Description="Meat" ,Quantity=(decimal)3.10});
lst.Add(new order{guid = "xxx",Parent="" ,ProductID="103",Description="Onion" ,Quantity=(decimal)8.00});
lst.Add(new order{guid = "zz5",Parent="z0",ProductID="105",Description="Meat" ,Quantity=(decimal)3.10});
lst.Add(new order{guid = "zz6",Parent="z0",ProductID="105",Description="Meat" ,Quantity=(decimal)3.10});
lst.Add(new order{guid = "zz4",Parent="z0",ProductID="105",Description="Meat" ,Quantity=(decimal)3.10});
lst.Add(new order{guid = "zz0",Parent="z0",ProductID="105",Description="Meat" ,Quantity=(decimal)4.10});
lst.Add(new order{guid = "0z3",Parent=null ,ProductID="100",Description="Abbb" ,Quantity=(decimal)1.10});
lst.Add(new order{guid = "0z2",Parent=null ,ProductID="100",Description="Abbb" ,Quantity=(decimal)1.10});
lst.Add(new order{guid = "0z1",Parent=null ,ProductID="100",Description="Abbb" ,Quantity=(decimal)1.10});
Console.WriteLine("\n-Consolid 01:");
var lstCons2 = lst.GroupBy(x => new
Parent = x.First().Parent,
ProductID = x.Key.ProductID,
Quantity = (decimal) x.Count(),
Total = (decimal) x.Sum(p => p.Quantity)
foreach(var it in lstCons2.OrderBy(x => x.ProductID))
Console.WriteLine( (it.ProductID ?? "").PadLeft(4, ' ') + ": " + (it.Parent?? "").PadLeft(4, ' ') + "; " + it.Quantity.ToString().PadLeft(5, ' ') + "; " + it.Total.ToString().PadLeft(5, ' ') + "; ");
Console.WriteLine("\n-Consolid 02:");
var lstAgrpTmp = lst.GroupBy(x => new
id = x.OrderBy(o => o.guid).First().guid,
ProductID = x.Key.ProductID,
CountVlr = (decimal) x.Count(),
Total = (decimal)x.Sum(p => p.Quantity)
foreach(var it in lstAgrpTmp.OrderBy(x => x.ProductID))
Console.WriteLine( "(" + (it.id ?? "").PadLeft(3, ' ') + ") " + (it.ProductID ?? "").PadLeft(4, ' ') + ": " + (it.Parent?? "").PadLeft(4, ' ') + "; " + it.CountVlr.ToString().PadLeft(5, ' ') + "; " + it.Total.ToString().PadLeft(5, ' ') + "; ");
Console.WriteLine("\n-Detalhe:");
foreach(var it in lst.OrderBy(x => x.ProductID).ThenBy(x=> x.Parent).ToList())
var x = Extensions.DeepClone<order>(it) ;
foreach(var it in lstFilhos.OrderBy(x => x.ProductID).ThenBy(x=> x.Parent).ToList())
var itCons = lstAgrpTmp.Where(x=> x.id == it.guid).FirstOrDefault();
var itFilho = lstFilhos.Where(x=> x.guid == it.guid).FirstOrDefault();
var itPai = Extensions.DeepClone<order>(itFilho) ;
itPai.Total = itCons.Total; itPai.Quantity = itCons.Total;
if( itCons.CountVlr > 1){
itPai.FlagConsolidado = true;
itFilho.FlagConsolidado = true;
Console.WriteLine("\n-Lista Pai:");
foreach(var it in lstFilhos.Where(x=> x.guidPai == null).OrderBy(x => x.ProductID).ThenBy(x=> x.Parent))
var itPai = lstAgrPai.Where(x=> (x.ProductID ?? "") == (it.ProductID ?? "") && (x.Parent ?? "") == (it.Parent ?? "") ).FirstOrDefault();
if( itPai.FlagConsolidado ){
foreach(var it in lstFilhos.OrderBy(x => x.guidPai).ThenBy(x=> x.guid))
if((it.guidPai ?? "").Equals("")){ it.nValidar = 2; continue;}
TotalFilho = lstFilhos.Where(x=> (!(x.guidPai ?? "").Equals("")) && (it.guidPai == x.guidPai) ).Sum(x => x.Quantity);
if(TotalFilho<=0 ) { it.nValidar = -1;}
} catch (Exception) { it.nValidar = -1;}
var itPai = lstAgrPai.Where(x=> (x.guid ?? "") == (it.guidPai ?? "") ).FirstOrDefault();
if (itPai.Quantity != TotalFilho){
} catch (Exception) { it.nValidar = -2;}
Console.WriteLine("\n-Lista Filhos:");
Console.WriteLine("\n-Detalhe:");
public string guid {get;set;}
public string guidPai {get;set;}
public string Parent {get;set;}
public string ProductID {get;set;}
public string Description{get;set;}
public decimal Quantity {get;set;}
public decimal Total {get;set;}
public bool FlagConsolidado {get;set;}
public int nValidar {get;set;}
public void ListarResumo(List<order> lst){
foreach(var it in lst.OrderBy(x => x.ProductID).ThenBy(x=> x.Parent))
Console.WriteLine( "(" + (it.guid ?? "").PadRight(4, ' ') + ") " +
"PId: " + (it.ProductID ?? "").PadRight(4, ' ') + ": " +
"Pai: " + (it.Parent?? "").PadRight(5, ' ') + "; " +
"Qtd: " + it.Quantity.ToString().PadLeft(5, ' ') + "; " +
"QtdTot: " + it.Total.ToString().PadLeft(5, ' ') + "; "
public void ListarId (List<order> lst1){
foreach(var it in lst1.OrderBy(x => x.ProductID).ThenBy(x=> x.Parent).ThenBy(x=> x.guidPai).ThenBy(x=> x.guid))
Console.WriteLine( " - " + it.FlagConsolidado.ToString().PadRight(5, ' ') + "; " +
"(" + (it.guidPai ?? "").PadRight(4, ' ') + ") " +
"(" + (it.guid ?? "").PadRight(4, ' ') + ") " +
"PId: " + (it.ProductID ?? "").PadLeft(4, ' ') + ": " +
"Pai: " + (it.Parent?? "").PadRight(5, ' ') + "; " +
"Qtd: " + it.Quantity.ToString().PadLeft(5, ' ') + "; " +
"QtdTot: " + it.Total.ToString().PadLeft(5, ' ') + "; " +
"Validar: " + it.nValidar.ToString().PadLeft(5, ' ') + "; "
Console.WriteLine(" - Total: " + lst1.Sum(x=>x.Total) + " ; Total.Qtde: " + lst1.Sum(x=>x.Quantity) + " ; Cont: " + lst1.Count);
namespace ExtensionMethods
public static class Extensions
public static bool IsGreaterThan(this int i, int value)
public static T DeepClone<T>(T obj)
using (MemoryStream ms = new MemoryStream())
BinaryFormatter bf = new BinaryFormatter();
objResult = (T)bf.Deserialize(ms);