Lambda and Linq Statements to pull all matching values from List


/ Published in: C#
Save to your folder(s)

These are statements to pull all the matching values (123) from the List.


Copy this code and paste it in your HTML
  1. List<string> testList = new List<string>(){"123","456","789","123","456","789","123","456","789","123","456","789","123","456","789"};
  2.  
  3. Lambda
  4. List<string> test = testList.Where(v => v == "123").ToList();
  5.  
  6. Linq
  7. List<string> test = (from x in testList
  8. where x == "123"
  9. select x).ToList();
  10.  
  11. foreach (var num in test)
  12. {
  13. Console.WriteLine(num);
  14. }
  15.  
  16. Console.ReadLine();
  17.  
  18. Results:
  19. "123"
  20. "123"
  21. "123"
  22. "123"
  23. "123"

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.