Advertisement
myname0

Linq

Apr 28th, 2016
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.23 KB | None | 0 0
  1. ublic static class Expansion
  2.     {
  3.         public static double Sum(this List<int> tmp)
  4.         {
  5.             double S = 0;
  6.             foreach (int i in tmp)
  7.                 S += i;
  8.             return S;
  9.         }
  10.     }
  11.  
  12.     public class Good
  13.     {
  14.         public string Title { get; set; }
  15.         public double Weight { get; set; }
  16.         public decimal Price { get; set; }
  17.         public DateTime DateOfArrival { get; set; }
  18.         public int StorageNumber { get; set; }
  19.  
  20.         //public Good(string _Title, double _Weight, decimal _Price, DateTime _DateOfArrival, int _StorageNumber)
  21.         //{
  22.         //    Title = _Title;
  23.         //    Weight = _Weight;
  24.         //    Price = _Price;
  25.         //    DateOfArrival = _DateOfArrival;
  26.         //    StorageNumber = _StorageNumber;
  27.         //}
  28.  
  29.         public void Print()
  30.         {
  31.             Console.WriteLine("{0} - {1} - {2} - {3} - {4}", Title, Weight, Price, DateOfArrival, StorageNumber);
  32.             Console.WriteLine();
  33.         }
  34.        
  35.     }
  36.  
  37.     public static class Program
  38.     {      
  39.         static void Main(string[] args)
  40.         {
  41.             List<int> arr1 = new List<int>(){ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
  42.             Console.WriteLine("Sum: {0}", arr1.Sum());
  43.             List<Good> goodslist = new List<Good>()
  44.             {
  45.                 new Good() { Title = "corn", Weight = 27.1 , Price = 21.5m, DateOfArrival = DateTime.Now.AddDays(1), StorageNumber = 1 },
  46.                 new Good() { Title = "ice", Weight = 30, Price = 13m, DateOfArrival = DateTime.Now.AddDays(2), StorageNumber = 1 },
  47.                 new Good() { Title = "car", Weight = 1, Price = 5.99m, DateOfArrival = DateTime.Now.AddDays(-3), StorageNumber = 2 },
  48.                 new Good() { Title = "toy", Weight = 15.7, Price = 3.75m, DateOfArrival = DateTime.Now.AddDays(6), StorageNumber = 3 },
  49.                 new Good() { Title = "biscuit", Weight = 12, Price = 43.6m, DateOfArrival = DateTime.Now.AddDays(8), StorageNumber = 2 }
  50.             };
  51.  
  52.             var emp = from goods in goodslist
  53.                       where goods.Price > 4
  54.                       orderby goods.Price descending
  55.                       select new
  56.                       {
  57.                           goods.Title,
  58.                           goods.Price
  59.                       };
  60.  
  61.             foreach (var item in emp)
  62.             {
  63.                 Console.WriteLine("{0} - {1}", item.Title, item.Price);
  64.             }
  65.  
  66.             var emp = from goods in goodslist
  67.                       where goods.DateOfArrival > DateTime.Now(-1)
  68.                       orderby goods.Price descending
  69.                       select new
  70.                       {
  71.                           goods.Title,
  72.                           goods.Price
  73.                       };
  74.             oderby
  75.  
  76.             foreach (var item in emp)
  77.             {
  78.                 Console.WriteLine("{0} - {1}", item.Title, item.Price);
  79.             }
  80.  
  81.  
  82.  
  83.             var group = from good in goodslist
  84.                         group good by good.StorageNumber;
  85.  
  86.             foreach (var item in group)
  87.             {
  88.                 Console.WriteLine(item.Key);
  89.                 foreach (var gr in item)
  90.                 {
  91.                     gr.Print();
  92.                 }
  93.                 Console.WriteLine();
  94.             }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement