우두머리 우두머리 - 2 months ago 7
C# Question

Contains doesn't work as I expected

I'm wondering why I cannot compare these objects:

public class myCustomClass
{
public string Value { get; set; }
public List<string> Keys { get; set; }
}


I receive a
List<myCustomClass>


I created a
Comparer
and looks like

var comparer = new myCustomClass{
Value = "somethingToCompare"
};


And when I do
MyList.Contains(comparer)
is
false
.

EDIT:
MyList
values have "somethingToCompare", but my comparer doesn't have keys so it's always false.

My question is, can I only compare if my value exists inside my List, without comparing my Keys?

Answer

I'm assuming MyQueryStringInfo and myCustomClass are the same?

Either way you are trying to find an object with the Value of "somethingToCompare" right?

If this is the case, try this instead:

MyList.Any(item => item.Value == comparer.Value);