Advertisement
Caminhoneiro

LINQ

Jul 16th, 2018
201
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.28 KB | None | 0 0
  1.     public class Customer
  2.     {
  3.         public int CustomerId { get; set; }
  4.         public string FirstName { get; set; }
  5.         public string LastName { get; set; }
  6.         public int? CustomerTypeId { get; set; }
  7.         public string EmailAddress { get; set; }
  8.  
  9.     }
  10.  
  11.  
  12.  
  13.  
  14. public class CustomerRepository
  15.     {
  16.         public Customer Find(List<Customer> customerList, int customerId)
  17.         {
  18.             Customer foundCustomer = null;
  19.  
  20.             //foreach (var c in customerList)
  21.             //{
  22.             //    if (c.CustomerId == customerId)
  23.             //    {
  24.             //        foundCustomer = c;
  25.             //        break;
  26.             //    }
  27.             //}
  28.  
  29.             ////Provides an IENumerable to a interaction
  30.             //var query = from c in customerList
  31.             //            where c.CustomerId == customerId
  32.             //            select c;
  33.  
  34.             //foundCustomer = query.First();
  35.  
  36.             //FirstOrDefault is a func delagate, A Func delegate defines a function that takes any number of parameters and returns a value of the
  37.             //type specified in the last parameter.
  38.             foundCustomer = customerList.FirstOrDefault(c =>
  39.                                 c.CustomerId == customerId);
  40.  
  41.             //foundCustomer = customerList.FirstOrDefault(c =>
  42.             //                {
  43.             //                    Debug.WriteLine(c.LastName);
  44.             //                    return c.CustomerId == customerId;
  45.             //                });
  46.  
  47.             //foundCustomer = customerList.Where(c =>
  48.             //                    c.CustomerId == customerId)
  49.             //                    .Skip(1)
  50.             //                    .FirstOrDefault();
  51.  
  52.             //foundCustomer = customerList.Where(c =>
  53.             //                    c.CustomerId == customerId)
  54.             //                    .Skip(1)
  55.             //                    .FirstOrDefault();
  56.  
  57.             return foundCustomer;
  58.  
  59.         }
  60.  
  61.         public List<Customer> Retrieve()
  62.         {
  63.             List<Customer> custList = new List<Customer>
  64.                     {new Customer()
  65.                           { CustomerId = 1,
  66.                             FirstName="Frodo",
  67.                             LastName = "Baggins",
  68.                             EmailAddress = "fb@hob.me",
  69.                             CustomerTypeId=1},
  70.                     new Customer()
  71.                           { CustomerId = 2,
  72.                             FirstName="Bilbo",
  73.                             LastName = "Baggins",
  74.                             EmailAddress = "bb@hob.me",
  75.                             CustomerTypeId=null},
  76.                     new Customer()
  77.                           { CustomerId = 3,
  78.                             FirstName="Samwise",
  79.                             LastName = "Gamgee",
  80.                             EmailAddress = "sg@hob.me",
  81.                             CustomerTypeId=1},
  82.                     new Customer()
  83.                           { CustomerId = 4,
  84.                             FirstName="Rosie",
  85.                             LastName = "Cotton",
  86.                             EmailAddress = "rc@hob.me",
  87.                             CustomerTypeId=2}};
  88.             return custList;
  89.         }
  90.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement