Posted By

heathbo on 11/14/13


Tagged


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

CSB_Campaigner


Lambda and Linq Statements to pull all matching values from List


 / Published in: C#
 

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

  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  

You need to login to post a comment.