简体   繁体   中英

Using LINQ, how would you filter out all but one item of a particular criteria from a list?

I realize my title probably isn't very clear so here's an example:

I have a list of objects with two properties, A and B.

public class Item
{
    public int A { get; set; }
    public int B { get; set; }
}

var list = new List<Item>
{
    new Item() { A = 0, B = 0 },
    new Item() { A = 0, B = 1 },
    new Item() { A = 1, B = 0 },
    new Item() { A = 2, B = 0 },
    new Item() { A = 2, B = 1 },
    new Item() { A = 2, B = 2 },
    new Item() { A = 3, B = 0 },
    new Item() { A = 3, B = 1 },
}

Using LINQ, what's the most elegant way to collapse all the A = 2 items into the first A = 2 item and return along with all the other items? This would be the expected result.

var list = new List<Item>
{
    new Item() { A = 0, B = 0 },
    new Item() { A = 0, B = 1 },
    new Item() { A = 1, B = 0 },
    new Item() { A = 2, B = 0 },
    new Item() { A = 3, B = 0 },
    new Item() { A = 3, B = 1 },
}

I'm not a LINQ expert and already have a "manual" solution but I really like the expressiveness of LINQ and was curious to see if it could be done better.

How about:

var collapsed = list.GroupBy(i => i.A)
                    .SelectMany(g => g.Key == 2 ? g.Take(1) : g);

The idea is to first group them by A and then select those again (flattening it with .SelectMany ) but in the case of the Key being the one we want to collapse, we just take the first entry with Take(1) .

One way you can accomplish this is with GroupBy . Group the items by A , and use a SelectMany to project each group into a flat list again. In the SelectMany , check if A is 2 and if so Take(1) , otherwise return all results for that group. We're using Take instead of First because the result has to be IEnumerable .

var grouped = list.GroupBy(g => g.A);
var collapsed = grouped.SelectMany(g =>
{
     if (g.Key == 2)
     {
         return g.Take(1);
     }
     return g;
});

One possible solution (if you insist on LINQ):

int a = 2;  
var output = list.GroupBy(o => o.A == a ? a.ToString() : Guid.NewGuid().ToString())
                 .Select(g => g.First())
                 .ToList();

Group all items with A=2 into group with key equal to 2 , but all other items will have unique group key (new guid), so you will have many groups having one item. Then from each group we take first item.

Yet another way:

var newlist = list.Where (l => l.A != 2 ).ToList();
newlist.Add( list.First (l => l.A == 2) );

An alternative to other answers based on GroupBy can be Aggregate :

// Aggregate lets iterate a sequence and accumulate a result (the first arg)
var list2 = list.Aggregate(new List<Item>(), (result, next) => {
     // This will add the item in the source sequence either
     // if A != 2 or, if it's A == 2, it will check that there's no A == 2
     // already in the resulting sequence!
     if(next.A != 2 || !result.Any(item => item.A == 2)) result.Add(next);  
     return result;
});

What about this:

list.RemoveAll(l => l.A == 2 && l != list.FirstOrDefault(i => i.A == 2));

if you whould like more efficient way it would be:

var first = list.FirstOrDefault(i => i.A == 2);
list.RemoveAll(l => l.A == 2 && l != first);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM