I want to compare a property instead of the entire object using a List[MyObject]. I therefore use IEquatable[MyObject] but the compiler still wants MyObject instead of the string property. Why?
Here is what I got:
public class AnyClass
{
public List<AnyOtherClass> MyProperty { get; set; }
public string AnyProperty { get; set; }
public AnyClass(string[] Names, string[] Values, string AnyProperty)
{
this.AnyProperty = AnyProperty;
this.MyProperty = new List<AnyOtherClass>();
for (int i = 0; i < Names.Length; i++)
MyProperty.Add(new AnyOtherClass(Names[i], Values[i]));
}
}
public class AnyOtherClass : IEquatable<string>
{
public AnyOtherClass(string Name, string Values)
{
this.Name = Name;
this.Values = Values.Split(';').ToList();
}
public string Name { get; set; }
public List<string> Values { get; set; }
public bool Equals(string other)
{
return this.Name.Equals(other);
}
}
private void DoSomething()
{
string[] Names = new string[] { "Name1", "Name2" };
string[] Values = new string[] { "Value1_1;Value1_2", "Value2" };
AnyClass ac = new AnyClass(Names, Values, "any Property");
if (ac.MyProperty.Contains("Name1")) //Problem is here...
//do something
}
Any
will stop evaluating when the first match is found, whereasWhere
will keep going to get a complete list (and potentially take a lot longer to run). – Wilds