using System.Collections.Generic;
public static void Main(string[] args)
Account pierre = new Account(1, "Pierre");
Account paul = new Account(2, "Paul");
Account jacques = new Account(3, "Jacques");
List<Data> credits = new List<Data>()
new Data() { BusinessUnitUID = 1, TransactionDate = DateTime.Today, Account = pierre, Amount = new Amount("REEL",150d) },
new Data() { BusinessUnitUID = 2, TransactionDate = DateTime.Today, Account = pierre, Amount = new Amount("REEL",20d) },
new Data() { BusinessUnitUID = 3, TransactionDate = new DateTime(2003,6,12), Account = paul, Amount = new Amount("PREV",55d) },
new Data() { BusinessUnitUID = 4, TransactionDate = new DateTime(2003,6,12), Account = paul, Amount = new Amount("REEL",38d) },
new Data() { BusinessUnitUID = 4, TransactionDate = new DateTime(2006,11,8), Account = paul, Amount = new Amount("PREV",320d) },
new Data() { BusinessUnitUID = 3, TransactionDate = new DateTime(2013,6,12), Account = jacques, Amount = new Amount("REEL",66d) },
new Data() { BusinessUnitUID = 4, TransactionDate = new DateTime(2004,5,4), Account = jacques, Amount = new Amount("REEL",112d) },
new Data() { BusinessUnitUID = 4, TransactionDate = new DateTime(2013,11,8), Account = jacques, Amount = new Amount("PREV",86d) },
List<Data> debits = new List<Data>()
new Data() { BusinessUnitUID = 1, TransactionDate = DateTime.Today, Account = pierre, Amount = new Amount("REEL",-20d) },
new Data() { BusinessUnitUID = 2, TransactionDate = DateTime.Today, Account = pierre, Amount = new Amount("PREV",-38d) },
new Data() { BusinessUnitUID = 3, TransactionDate = new DateTime(2003,6,12), Account = paul, Amount = new Amount("PREV",-25d) },
new Data() { BusinessUnitUID = 4, TransactionDate = new DateTime(2003,6,12), Account = paul, Amount = new Amount("REEL",-60d) },
new Data() { BusinessUnitUID = 4, TransactionDate = new DateTime(2006,11,8), Account = paul, Amount = new Amount("REEL",-65d) },
new Data() { BusinessUnitUID = 3, TransactionDate = new DateTime(2013,6,12), Account = jacques, Amount = new Amount("PREV",-165d) },
new Data() { BusinessUnitUID = 4, TransactionDate = new DateTime(2004,5,4), Account = jacques, Amount = new Amount("REEL",-85d) },
new Data() { BusinessUnitUID = 4, TransactionDate = new DateTime(2013,11,8), Account = jacques, Amount = new Amount("PREV",-32d) },
List<AccountDatesAmount> adaCredits = DataToAccountDatesAmount(credits);
List<AccountDatesAmount> adaDebits = DataToAccountDatesAmount(debits);
List<AccountDatesAmount> transactionsDateCompte = adaCredits.Concat(adaDebits).GroupBy(g => new { g.Date, g.Account })
.Select(x => new AccountDatesAmount()
Amounts = Amount.MergeAmounts(x.SelectMany(y => y.Amounts))
AfficherBilan(transactionsDateCompte);
private static List<AccountDatesAmount> DataToAccountDatesAmount(List<Data> datas)
return datas.GroupBy(g => new { g.TransactionDate.Date, g.Account })
.Select(x => new AccountDatesAmount()
Amounts = x.GroupBy(f => f.Amount.Type)
.Select(v => new Amount(v.Key, v.Sum(y => y.Amount.Somme)))
private static void AfficherBilan(List<AccountDatesAmount> transactionsDateCompte)
Console.Write("Date".PadRight(12));Console.Write("compte".PadRight(12));Console.Write("Balance".PadRight(12));
foreach (var tdc in transactionsDateCompte)
Console.Write(tdc.Date.ToShortDateString().PadRight(12));
Console.Write(tdc.Account.AccountName.PadRight(12));
foreach (var operation in tdc.Amounts)
Console.Write(operation.Type.PadRight(6));
Console.Write(operation.Somme.ToString().PadRight(6));
public int BusinessUnitUID { get; set; }
public DateTime TransactionDate { get; set; }
public Account Account { get; set; }
public Amount Amount { get; set; }
class Account : IEqualityComparer<Account>
public Account(int id, string accountName)
AccountName = accountName;
public int Id { get; set; }
public string AccountName { get; set; }
public bool Equals(Account x, Account y)
public int GetHashCode(Account obj)
return obj.Id.GetHashCode();
public Amount(string type, double somme)
public string Type { get; set; }
public double Somme { get; set; }
public static List<Amount> MergeAmounts(IEnumerable<Amount> amounts)
return amounts.GroupBy(a => a.Type)
.Select(v => new Amount(v.Key, v.Sum(y => y.Somme)))
public DateTime Date { get; set; }
public Account Account { get; set; }
public List<Amount> Amounts { get; set; }