Advertisement
QwarkDev

LINQ Example

Aug 26th, 2020
1,538
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.77 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace skit_d_inaction
  8. {
  9.     class Program
  10.     {
  11.         static void WriteCollection(Object[] collection)
  12.         {
  13.             foreach (var t in collection)
  14.             {
  15.                 Console.WriteLine(t);
  16.             }
  17.  
  18.             Console.WriteLine(Environment.NewLine);
  19.         }
  20.  
  21.         static void Main(string[] args)
  22.         {
  23.             var products = Product.GetSamples();
  24.             var suppliers = Supplier.GetSamples();
  25.  
  26.             var filtered0 = from p in products
  27.                            join s in suppliers
  28.                              on p.SupplierID equals s.ID
  29.                            select new { SupplierName = s.Name, ProductName = p.Name };
  30.  
  31.             var filtered1 = from p in products
  32.                             join s in suppliers
  33.                               on p.SupplierID equals s.ID
  34.                             where p.Price > 1000
  35.                             orderby p.Name, s.Name
  36.                             select new { SupplierName = s.Name, ProductName = p.Name };
  37.  
  38.             WriteCollection(filtered0.ToArray());
  39.             WriteCollection(filtered1.ToArray());
  40.         }
  41.     }
  42.  
  43.     class Supplier
  44.     {
  45.         public string Name { get; set; }
  46.         public uint ID { get; set; }
  47.  
  48.         public static Supplier[] GetSamples()
  49.         {
  50.             return new Supplier[]
  51.             {
  52.                 new Supplier() { ID = 0, Name = "Apple" },
  53.                 new Supplier() { ID = 1, Name = "Samsung" },
  54.                 new Supplier() { ID = 2, Name = "Microsoft" },
  55.                 new Supplier() { ID = 3, Name = "Matarola inc" }
  56.             };
  57.         }
  58.     }
  59.  
  60.     class Product
  61.     {
  62.         public string  Name   { get; set; }
  63.         public decimal Price  { get; set; }
  64.         public uint SupplierID { get; set; }
  65.  
  66.         public Product(string name, string price, uint supplierID)
  67.         {
  68.             Name = name;
  69.             Price = decimal.Parse(price);
  70.         }
  71.  
  72.         public Product() { }
  73.  
  74.         public override string ToString()
  75.         {
  76.             return $"{Name}: {Price}";
  77.         }
  78.  
  79.         public static Product[] GetSamples()
  80.         {
  81.             return new Product[]
  82.             {
  83.                 new Product() { Name = "Product #1", Price = 1000, SupplierID = 0 },
  84.                 new Product() { Name = "Product #2", Price = 2000, SupplierID = 0 },
  85.                 new Product() { Name = "Product #3", Price = 3000, SupplierID = 1 },
  86.                 new Product() { Name = "Product #4", Price = 4000, SupplierID = 2 },
  87.                 new Product() { Name = "Product #5", Price = 5000, SupplierID = 3 }
  88.             };
  89.         }
  90.     }
  91. }
  92.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement