code_junkie

What's the hardest or most misunderstood aspect of LINQ? [closed]

Nov 11th, 2011
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.42 KB | None | 0 0
  1. static void Linq_Deferred_Execution_Demo()
  2. {
  3. List<String> items = new List<string> { "Bob", "Alice", "Trent" };
  4.  
  5. var results = from s in items select s;
  6.  
  7. Console.WriteLine("Before add:");
  8. foreach (var result in results)
  9. {
  10. Console.WriteLine(result);
  11. }
  12.  
  13. items.Add("Mallory");
  14.  
  15. //
  16. // Enumerating the results again will return the new item, even
  17. // though we did not re-assign the Linq expression to it!
  18. //
  19.  
  20. Console.WriteLine("nAfter add:");
  21. foreach (var result in results)
  22. {
  23. Console.WriteLine(result);
  24. }
  25. }
  26.  
  27. Before add:
  28. Bob
  29. Alice
  30. Trent
  31.  
  32. After add:
  33. Bob
  34. Alice
  35. Trent
  36. Mallory
  37.  
  38. Single(x => x.id == id)
  39.  
  40. Where(x => x.id == id).Single()
  41.  
  42. [TestMethod]
  43. public void QueryComplexityTest()
  44. {
  45. var users = _dataContext.Users;
  46.  
  47. Func<User, bool> funcSelector = q => q.UserName.StartsWith("Test");
  48. Expression<Func<User, bool>> expressionSelector = q => q.UserName.StartsWith("Test");
  49.  
  50. // Returns IEnumerable, and do filtering of data on client-side
  51. IQueryable<User> func = users.Where(funcSelector).AsQueryable();
  52. // Returns IQuerible and do filtering of data on server side
  53. // SELECT ... FROM [dbo].[User] AS [t0] WHERE [t0].[user_name] LIKE @p0
  54. IQueryable<User> exp = users.Where(expressionSelector);
  55. }
  56.  
  57. from outerloopitem in outerloopitems
  58. from innerloopitem in outerloopitem.childitems
  59. select outerloopitem, innerloopitem
Add Comment
Please, Sign In to add comment