Advertisement
wjcustode

Some Linq Tips

Sep 12th, 2015
29
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.61 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 BlogCode
  8. {
  9.     public class Person
  10.     {
  11.         public int Id { get; set; }
  12.  
  13.         public string FirstName { get; set; }
  14.  
  15.         public string LastName { get; set; }
  16.  
  17.         public int Age { get; set; }
  18.  
  19.         public IEnumerable<Address> Addresses { get; set; }
  20.     }
  21.  
  22.     public class Address
  23.     {
  24.         public int Id { get; set; }
  25.  
  26.         public int HouseNumber { get; set; }
  27.  
  28.         public string Street { get; set; }
  29.  
  30.         public string City { get; set; }
  31.  
  32.         public string State { get; set; }
  33.  
  34.         public string ZipCode { get; set; }
  35.     }
  36.  
  37.     class Program
  38.     {
  39.         static void Main(string[] args)
  40.         {
  41.             IEnumerable<Person> people = new List<Person>();
  42.  
  43.             var numberOfSteves = people.Count(p => p.FirstName == "Steve");
  44.  
  45.             var hasZPeople = people.Any(p => p.LastName.StartsWith("Z"));
  46.  
  47.             var canEveryoneDrink = people.All(p => p.Age > 21);
  48.  
  49.             var allAddresses = people.SelectMany(p => p.Addresses);
  50.  
  51.             var person3 = people.SingleOrDefault(p => p.Id == 3);
  52.  
  53.             var firstName = people.Select(p => p.FirstName);
  54.             var lastName = people.Select(p => p.LastName);
  55.  
  56.             firstName.Zip(lastName, (first, last) => new Person
  57.             {
  58.                 FirstName = first,
  59.                 LastName = last
  60.             });
  61.  
  62.             people.SelectMany(p => p.Addresses.Select(a => new
  63.             {
  64.                 p.FirstName,
  65.                 p.LastName,
  66.                 a.HouseNumber,
  67.                 a.Street,
  68.                 a.City,
  69.                 a.State,
  70.                 a.ZipCode
  71.             }));
  72.  
  73.             people = new List<Person>
  74.                 {
  75.                     new Person
  76.                     {
  77.                         Id = 1,
  78.                         FirstName = "Jen"
  79.                     },
  80.                     new Person
  81.                     {
  82.                         Id = 2,
  83.                         FirstName = "Will"
  84.                     },
  85.                     new Person
  86.                     {
  87.                         Id = 3,
  88.                         FirstName = "Jen"
  89.                     }
  90.                 };
  91.  
  92.             var aa = people.First(); // id: 1, FirstName: Jen
  93.             var bb = people.FirstOrDefault(); // id: 1, FirstName: Jen
  94.             var cc = people.Single(); // Exception!
  95.             var dd = people.SingleOrDefault(); // Exception!
  96.  
  97.             var ee = people.First(p => p.FirstName == "Jen"); // id: 1, FirstName: Jen
  98.             var ff = people.FirstOrDefault(p => p.FirstName == "Jen"); // id: 1, FirstName: Jen
  99.             var gg = people.Single(p => p.FirstName == "Jen"); // Exception!
  100.             var hh = people.SingleOrDefault(p => p.FirstName == "Jen"); // Exception!
  101.  
  102.             var ii = people.First(p => p.FirstName == "Will"); // id: 2, FirstName: Will
  103.             var jj = people.FirstOrDefault(p => p.FirstName == "Will"); // id: 2, FirstName: Will
  104.             var kk = people.Single(p => p.FirstName == "Will"); // id: 2, FirstName: Will
  105.             var ll = people.SingleOrDefault(p => p.FirstName == "Will"); // id: 2, FirstName: Will
  106.  
  107.             var mm = people.First(p => p.FirstName == "Steve"); // Exception!
  108.             var nn = people.FirstOrDefault(p => p.FirstName == "Steve"); // null
  109.             var oo = people.Single(p => p.FirstName == "Steve"); // Exception!
  110.             var pp = people.SingleOrDefault(p => p.FirstName == "Steve"); // null
  111.         }
  112.     }
  113. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement