I have two different list types. I need to remove the elements from list1 that is not there in list2 and list2 element satisfies certain criteria.
Here is what I tried, seems to work but each element is listed twice.
var filteredTracks =
from mtrack in mTracks
join ftrack in tracksFileStatus on mtrack.id equals ftrack.Id
where mtrack.id == ftrack.Id && ftrack.status == "ONDISK" && ftrack.content_type_id == 234
select mtrack;
Ideally I don’t want to create a new copy of the filteredTracks, is it possible modify mTracks in place?
If you’re getting duplicates, it’s because your
idfields are not unique in one or both of the two sequences. Also, you don’t need to saywhere mtrack.id == ftrack.Idsince that condition already has to be met for the join to succeed.I would probably use loops here, but if you are dead set on LINQ, you may need to group
tracksFileStatusby itsIdfield. It’s hard to tell by what you posted.As far as “modifying
mTracksin place”, this is probably not possible or worthwhile (I’m assuming thatmTracksis some type derived fromIEnumerable<T>). If you’re worried about the efficiency of this approach, then you may want to consider using another kind of data structure, like a dictionary withIdvalues as the keys.