Remove items from list 1 not in list 2

2019-03-12 06:25发布

I am learning to write lambda expressions, and I need help on how to remove all elements from a list which are not in another list.

var list = new List<int> {1, 2, 2, 4, 5};
var list2 = new List<int> { 4, 5 };

// Remove all list items not in List2
// new List Should contain {4,5}    
// The lambda expression is the Predicate.
list.RemoveAll(item => item. /*solution expression here*/ );

// Display results.
foreach (int i in list)
{
    Console.WriteLine(i);
}

标签: c# lambda
4条回答
欢心
2楼-- · 2019-03-12 06:33

This question has been marked as answered, but there is a catch. If your list contains an object, rather than a scalar, you need to do a bit more work.

I tried this over and over with Remove() and RemoveAt() and all sorts of things and none of them worked correctly. I couldn't even get a Contains() to work correctly. Never matched anything. I was stumped until I got the suspicion that maybe it could not match up the item correctly.

When I realized this, I refactored the item class to implement IEquatable, and then it started working.

Here is my solution:

class GenericLookupE : IEquatable<GenericLookupE>
{
    public string   ID  { get; set; }

    public bool     Equals( GenericLookupE other )
    {
        if ( this.ID == other.ID )      return true;

        return false;
    }
}

After I did this, the above RemoveAll() answer by Reed Copsey worked perfectly for me.

See: http://msdn.microsoft.com/en-us/library/bhkz42b3.aspx

查看更多
我想做一个坏孩纸
3楼-- · 2019-03-12 06:40

You can do this via RemoveAll using Contains:

list.RemoveAll( item => !list2.Contains(item));

Alternatively, if you just want the intersection, using Enumerable.Intersect would be more efficient:

list = list.Intersect(list2).ToList();

The difference is, in the latter case, you will not get duplicate entries. For example, if list2 contained 2, in the first case, you'd get {2,2,4,5}, in the second, you'd get {2,4,5}.

查看更多
We Are One
4楼-- · 2019-03-12 06:45
list = list.Except(list2).ToList();
查看更多
狗以群分
5楼-- · 2019-03-12 06:54

Easier solution for objects than horace gave:

If your list contains an object, rather than a scalar, it is that simple, by removing by one selected property of the objects:

    var a = allActivePatientContracts.RemoveAll(x => !allPatients.Select(y => y.Id).Contains(x.PatientId));
查看更多
登录 后发表回答