Reputation: 11735
I've got 2 generic lists that do not contain the all fields of the same type
IList<Category> and List<CategoriesRow>Categories
categoryList = IList<Category>
but both have common fields Name and ID.
I want to compare list Categories with categoryList and find those from categoryList where categoryList[index].ID does not exist in the list of all Categories of ID. For all those that do not exist in Categories, I will have to remove them from CatgoryList.
I had a previous post in which I was given examples of LINQ, but the problem is I have to use Dynamic, implying that I am passing categoryList and Categories as Dynamic.
Can anyone give me an example how to go about the above as I have no idea how to do it.
Upvotes: 2
Views: 4596
Reputation: 2157
let say you have a list1
and list2
like this:
IList<Category> list1 = new List<Category>();
IList<CategoryRow> list2 = new List<CategoryRow>();
//populate lists here....
Now to select from list1
only those items that have a matching Id
in list2
, you could do
list1 = list1.Where(c => list2.Exists(cr => cr.Id == c.Id)).ToList();
Hope that answers your question
Upvotes: 1