C# LINQ get objects with field value that do not match a string array blacklist

c#, linq

Solution

Try this:

 var objects = 
      myObjects.Where(m => !namesToExclude.Contains(m.Name)).ToList();

Problem

I have the following code: ``` public class MyObject { public string Name { get; set; } public MyObject(string name) { Name = name; } } // caller method somewhere public void myMethod() { List<MyObject> myObjects = new List<MyObject>(); myObjects.Add(new MyObject("Jim")); myObjects.Add(new MyObject("David")); myObjects.Add(new MyObject("Richard")); myObjects.Add(new MyObject("Steve")); string[] namesToExclude = new string[] { "Jim", "Steve" }; List<string> strings = myObjects.Select(m => m.Name).Except(namesToExclude).ToList(); // replace the above line to result in List<MyObject> with 2 items (David, Richard) } ``` I'd like to get this last line working so I can get back the list as a `List<MyObject>` rather than as a `List<string>`.

Original source