Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Aug 10th, 2012  |  syntax: None  |  size: 0.89 KB  |  hits: 10  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. LINQ - Contains in?
  2. private ObservableCollection<User> userCollection = someData;
  3.  
  4. public class User
  5. {
  6.     public string Username { get; set; }
  7.     public ObservableCollection<Department> Memberships { get; set; }
  8. }
  9.  
  10. public class Department
  11. {
  12.     public string Name { get; set; }
  13. }
  14.  
  15. //I can't use "Contains", because it requires an object of type "Department", but I don't have the object, just the string "Name" (in this case "MyDepartment")
  16. var result = from usr in userCollection where (usr.Memberships.Contains("MyDepartment")) select usr;
  17.        
  18. (from usr in userCollection select usr).
  19.                Any(x=>x.Username.Contains("MyDepartment"))
  20.        
  21. var result = from usr in userCollection
  22.              where usr.Memberships.Any(m => m.Name == "MyDepartment")
  23.              select usr;
  24.        
  25. var result = from usr in userCollection where (usr.Memberships.Select(m=>m.Name).Contains("MyDepartment")) select usr;