0

I need to match items in this list, however, the Players value is not always known. How can I exclude the Players from the myMembers.Contains match ? Or, how can I only search by ID (which will always be available and unique)?

public class Data
{
    public Data(string id, string firstName, string lastName, string players)
    {
        this.Id = id;
        this.FirstName = firstName;
        this.LastName = lastName;
        this.PLayers= players;
    }
    public string Id{set;get;}
    public string FirstName{ set; get; }
    public string LastName { set; get; }
    public string Players{ set; get; }
}
List<Data> myMembers= new List<Data>();
if(myMembers.Contains(new Data(Id, First, Last, Players))
{
   myMembers.Remove(new Data(Id, First, Last, Players));
}

1 Answers1

0

If Id is unique, you can simply compare that to find out what should be removed.

var match = myMembers.FirstOrDefault(m => m.Id == Id);

If(match != null)
{
   myMembers.Remove(match);
}
ProgrammingLlama
  • 36,677
  • 7
  • 67
  • 86
Sajjad Mortazavi
  • 190
  • 2
  • 12