我有以下代码:
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)
}
我想让最后一行正常工作,这样我就可以将列表作为 aList<MyObject>
而不是作为List<string>
.