I have an object like this -
public MyObject
{
public int Id { get; set; }
public string MyType { get; set; }
}
a list of those objects -
public List<MyObject> MyObjectList { get; set; }
and a list of string like this -
public List<string> MyTypeList { get; set; }
Using LINQ on MyObjectList
, I want to create a list of MyObject
removing any MyObject
that has a MyObject.MyType
that is in the MyTypeList
.
Something like this (here are some unsuccessful attempts) -
List<MyObject> MyObjectListNEW = MyObjectList.Select(i => i.MyType).Except(MyTypeList).ToList();
List<MyObject> MyObjectListNEW = MyObjectList.Where(i => i.MyType.Except(MyTypeList));
CodePudding user response:
This should work:
var MyObjectList = new List<MyObject>();
var MyTypeList = new List<string>();
var results = MyObjectList.Where(m => !MyTypeList.Any(t => m.MyType == t)).ToList();
CodePudding user response:
I think it should be
var results = MyObjectList.Where(m => ! MyTypeList.Contains(m.MyType)).ToList();
CodePudding user response:
try this
var results = MyObjectList.Where(m => !MyTypeList.Contains(m.MyType)).ToList();