-4

I have list of countries in DetailList.country and I would like to remove Russia from this collection if it exists in the collection. Any in-built function available in C#?

Here collection is System.Collections.ObjectModel.Collection

DetailListclass:

public class DetailList
    {
public string FirstName { get; set; }
        public string LastName { get; set; }
        public string Email { get; set; }
        public string Country { get; set; }
}

Collection<DetailList> list = new Collection<DetailList>();
Tech Learner
  • 1,227
  • 6
  • 24
  • 59
  • 5
    Did you ever try typing `countrylist.` and then seeing what popped up in the autocomplete list after you type the dot? – slugster Sep 26 '17 at 11:11
  • @slugster - Yes. But there is no remove option in the context. – Tech Learner Sep 26 '17 at 11:13
  • 2
    The `Collection` class provides a method called `Remove()`, which you should also be able to see in your intellisense (as slugster pointed out). See the following link to MSDN for its documentation: https://msdn.microsoft.com/en-us/library/ms132413(v=vs.110).aspx – bassfader Sep 26 '17 at 11:16
  • @bassfader - First I need to check whether it exists or not. – Tech Learner Sep 26 '17 at 11:18
  • check out `Contains`... – Zohar Peled Sep 26 '17 at 11:20
  • 1
    And the `EntityContact` class, not sure why there are two classes here. – Patrick Hofman Sep 26 '17 at 11:23
  • Like the others have indicated already, use Contains and Remove. Here's the code if it were a list of string as you haven't posted your classes, change it to match your types: if (countrylist.Any(cl => cl == "Russia")) countrylist.Remove("Russia"); – elloco999 Sep 26 '17 at 11:26
  • You can use linq to see if Russia is in the collection: if (countrylist.Any(cl => cl.Country == "Russia")) – elloco999 Sep 26 '17 at 11:34
  • @Ask_SO Are you looking for 1 and only 1 item to remove, or do you want to remove all items that have Country == "Russia" from the list? – elloco999 Sep 26 '17 at 11:37
  • Also, is there a reason you are using Collection instead of List? – elloco999 Sep 26 '17 at 11:39
  • @elloco999 Thanks a lot. Your code works perfectly. One more question, how to add a country to this list again? – Tech Learner Sep 26 '17 at 11:41
  • countrylist.Add(new CountyList() { Country = "Russia", FirstName = "Ivan"}); ? – elloco999 Sep 26 '17 at 11:42
  • You're welcome. One note though: if you use List instead of collection, you can remove all items where Country = "Russia" in one quick linq statement, no to check if any exist: detaillist.RemoveAll(cl => cl.Country == "Russia"); That is of course assuming you want to remove all items where Country = "Russia" Anyway, with List you have more options with linq. Of course you can always use ToList, but why not use List in the first place? – elloco999 Sep 26 '17 at 11:54
  • @elloco999 - These methods were already written. I am just altering the logic. – Tech Learner Sep 26 '17 at 12:02
  • @elloco999 `Collection` also implements `IEnumerable`... – Zohar Peled Sep 26 '17 at 12:29
  • @ZoharPeled You can't do Collection.RemoveAll() according to my Visual Studio, but you can do List/.RemoveAll(). – elloco999 Sep 26 '17 at 13:02

1 Answers1

1

To recap : System.Collections.ObjectModel.Collection<T> have a few methods you can use for this.

To check if an item exists in a collection use the Contains method.
To remove an item from the collection use the Remove method.

Please note that you can use the Remove method even if the item doesn't exist in the collection, and it will just return false.

However, if you insist of checking if the item actually exists in the collection before removing it, I would suggest using IndexOf and RemoveAt, since it will save you one search over the collection.

Since I'm not sure how to read your code sample, I will use a collection of strings for this demo code:

var stringCollection = new Collection<string>();
// Populate here

var index = stringCollection.IndexOf("Russia");
if(index > -1)
{
    stringCollection.RemoveAt(index);
    Console.WriteLine("Russia removed from the collection");
}
else
{
    Console.WriteLine("Russia not found in the collection");
}
Zohar Peled
  • 79,642
  • 10
  • 69
  • 121
  • Why not recommend to use `List`? – Patrick Hofman Sep 26 '17 at 11:44
  • While this code is absolutely correct when working with a Collection, how do you implement this for a Collection (see DetailList class in OP's post)? You'd need to create a DetailList object with exactly the same fields as the one you are trying to remove. And if you want to remove all items that have Country = "Russia" Contains is not going to help you. – elloco999 Sep 26 '17 at 11:47
  • @elloco999 As far as I understand the documentation, it's enough to override the `Equals` (and the `GetHashCode` sequentially) methods in the object in question. – Zohar Peled Sep 26 '17 at 12:22
  • @PatrickHofman What's the benefit of `List` over `Collection` in this case? As far as I can see, both implements all the methods I've mentioned in my answer. (Though thinking about it, List also implements `BinarySearch` which is O(log n) as apposed to `IndexOf` which is O(n)...) – Zohar Peled Sep 26 '17 at 12:27
  • @ZoharPeled I tried overriding the Equals and GetHashCode methods. That does work but does require you to instantiate a new DetailsList object with Country = "Russia". And it has the nasty side effect of no longer being able to equate to DetailsList objects if you wanted to. – elloco999 Sep 26 '17 at 13:29
  • Yeah, well, I'm not sure it's worth the time we are spending on it, since this question is already closed as a duplicate.... – Zohar Peled Sep 26 '17 at 14:11