MagusZero

The difference between Select and SelectMany

Oct 16th, 2013
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.63 KB | None | 0 0
  1. #region Terrible Object
  2.  
  3. var hasAllTheItems =
  4.     new[]
  5.     {
  6.         new[]
  7.         {
  8.             new
  9.             {
  10.                 Name = "Test"
  11.             }
  12.         },
  13.         new[]
  14.         {
  15.             new
  16.             {
  17.                 Name = "Test2"
  18.             },
  19.             new
  20.             {
  21.                 Name = "Test3"
  22.             }
  23.         }
  24.     };
  25.  
  26. #endregion Terrible Object
  27.  
  28. var a = hasAllTheItems.Select(x => x.Select(y => y.Name));
  29. var b = hasAllTheItems.SelectMany(x => x.Select(y => y.Name));
  30. var c = hasAllTheItems.Select(x => x.SelectMany(y => y.Name));
  31. var d = hasAllTheItems.SelectMany(x => x.SelectMany(y => y.Name));
  32.  
  33. Assert.AreEqual(2, a.Count());
  34. Assert.AreEqual(3, b.Count());
  35. Assert.AreEqual(2, c.Count());
  36. Assert.AreEqual(14, d.Count());
Advertisement
Add Comment
Please, Sign In to add comment