Advertisement
Guest User

Untitled

a guest
Feb 8th, 2016
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.10 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace ConsoleApplication2
  6. {
  7.     public class Test
  8.     {
  9.         public static void Main(string[] args)
  10.         {
  11.             var bills = new List<Bill>
  12.             {
  13.                 new Bill {date = DateTime.Now.Date.AddDays(-5), bill = 111, value = 10000},
  14.                 new Bill {date = DateTime.Now.Date.AddDays(-4), bill = 111, value = 15000},
  15.                 new Bill {date = DateTime.Now.Date.AddDays(-4), bill = 222, value = 10000},
  16.                 new Bill {date = DateTime.Now.Date.AddDays(-5), bill = 333, value = 15000},
  17.                 new Bill {date = DateTime.Now.Date.AddDays(-5), bill = 444, value = 10000},
  18.                 new Bill {date = DateTime.Now.Date.AddDays(-5), bill = 555, value = 15000},
  19.                 new Bill {date = DateTime.Now.Date.AddDays(-1), bill = 222, value = 15000},
  20.                 new Bill {date = DateTime.Now.Date.AddDays(-1), bill = 111, value = 10000}
  21.             };
  22.  
  23.  
  24.             var clientBills = new List<ClientBill>
  25.             {
  26.                 new ClientBill {LastName = "Иванов", billNumber = 111, city = "Москва"},
  27.                 new ClientBill {LastName = "Иванов", billNumber = 222, city = "Москва"},
  28.                 new ClientBill {LastName = "Петров", billNumber = 333, city = "не Москва"},
  29.                 new ClientBill {LastName = "Петров", billNumber = 444, city = "не Москва"},
  30.                 new ClientBill {LastName = "Сидоров", billNumber = 555, city = "Москва"}
  31.             };
  32.  
  33.             var result =
  34.                 clientBills.Where(x => x.city == "Москва")
  35.                     .Select(x => x.LastName)
  36.                     .Distinct()
  37.                     .ToDictionary(client => client,
  38.                         client =>
  39.                             clientBills.Where(x => x.LastName == client)
  40.                                 .Select(x => x.billNumber)
  41.                                 .Sum(
  42.                                     bill =>
  43.                                         bills.Select(x => x.bill)
  44.                                             .Distinct()
  45.                                             .Select(
  46.                                                 b =>
  47.                                                     bills.Where(x => x.bill == b)
  48.                                                         .OrderByDescending(x => x.date)
  49.                                                         .FirstOrDefault())
  50.                                             .Where(x => x.bill == bill)
  51.                                             .Select(x => x.value)
  52.                                             .FirstOrDefault())).Where(x => x.Value > 20000);
  53.         }
  54.     }
  55.  
  56.     public class Bill
  57.     {
  58.         public DateTime date { get; set; }
  59.         public int bill { get; set; }
  60.         public double value { get; set; }
  61.     }
  62.  
  63.     public class ClientBill
  64.     {
  65.         public string LastName { get; set; }
  66.         public int billNumber { get; set; }
  67.         public string city { get; set; }
  68.     }
  69. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement