I'm desperately trying to delete all the items with a list of the same value inside.
Here's the code:
private void Button_deleteDouble_MouseDown(object sender, EventArgs e)
{
boardGenerate.Add(new BoardInformation(146, new List<string> { "test" }));
boardGenerate.Add(new BoardInformation(545, new List<string> { "test" }));
boardGenerate = boardGenerate.DistinctBy(x => x.positionQueen).ToList();
}
Normally, since the two lists inside the object are the same, the .DistinctBy() command should remove one of the two objects.
But no, my object list still has the same two objects with the same list
.positionQueen is the name of the variable containing the list
Could somebody help me?
Edit :
The DistinctBy() method comes from MoreLinq.
And this is my BoardInformation class:
public class BoardInformation
{
public BoardInformation(int nbQueen, List<string> positionQueen)
{
this.nbQueen = nbQueen;
this.positionQueen = positionQueen;
}
public int nbQueen { get; set; }
public List<string> positionQueen { get; set; }
}

BoardInformation. Please provide a minimal reproducible example. But note thatList<T>doesn't overrideEqualsorGetHashCode. IfpositionQueenis theList<string>, you probably need to provide anIEqualityComparer<List<string>>to theDistinctBycall. (It would also help if you'd say whichDistinctBymethod this is - is it MoreLINQ?)Distinct, why would it delete?boardGenerate[0].positionQueen.Equals(boardGenerate[1].positionQueen)and it will print False, becauseList<T>doesn't overrideEquals- and that's whatDistinctByuses to find equal values.