1

I want to remove objects from a list using linq, for example :

public class Item
{
    public string number;
    public string supplier;
    public string place;
}

Now i want to remove all of the items with the same number and supplier that appear more then once.
Thanks

2
  • 2
    As for me the question is not clear Commented Jul 19, 2011 at 14:23
  • I assume appear in the least more then one should read appear in the list more than once Commented Jul 19, 2011 at 14:26

1 Answer 1

5

This would be slightly tedious to do in-place, but if you don't mind creating a new list, you can use LINQ.

The easiest way to specify your definition of item-equality is to project to an instance of an anonymous-type - the C# compiler adds a sensible equality-implementation on your behalf:

List<Item> items = ...

// If you mean remove any of the 'duplicates' arbitrarily. 
List<Item> filteredItems = items.GroupBy(item => new { item.number, item.supplier })
                                .Select(group => group.First())
                                .ToList();

// If you mean remove -all- of the items that have at least one 'duplicate'.
List<Item> filteredItems = items.GroupBy(item => new { item.number, item.supplier })
                                .Where(group => group.Count() == 1)
                                .Select(group => group.Single())
                                .ToList();

If my first guess was correct, you can also consider writing an IEqualityComparer<Item> and then using the Distinct operator:

IEqualityComparer<Item> equalityComparer = new NumberAndSupplierComparer();
List<Item> filteredItems = items.Distinct(equalityComparer).ToList();

Btw, it's not conventional for types to expose public fields (use properties) or for public members to have camel-case names (use pascal-case). This would be more idiomatic:

public class Item
{
    public string Number { get; set; }
    public string Supplier { get; set; }
    public string Place { get; set; }
}
Sign up to request clarification or add additional context in comments.

4 Comments

i will try that, nice job for the quick thinking.
Is it possible to use it without the new { item.NUmber , item.Supplier} and keep the original class ?
Let me just say that what is very hard on the one hand could be a lot easier on the other, meaning (that i am an idiot) let pick the items with more then 1 rather remove the items that are distinct. now how can i group them by more then on property ?
To group them by more than one property, the easiest way is to use the anonymous-type projection like I've mentioned. To pick the ones that have at least 1 dupe, try: items.GroupBy(--).Where(group => group.Count() > 1).SelectMany(group => group).ToList()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.