How to use IList.Contains() method to find an object

淺唱寂寞╮ 提交于 2019-12-25 02:34:10

问题


I have a list of Persons inside a Company Class.

public class Company{
    IList<Person> persons;
}

Then I have a List of companies,

IList<Company> companies;

Now I have a name (say "Lasantha"). If this name is a part of the name of any person in a company, I want to find that company. I tried using companies.Contains() method. I overrided the object.Equals method, inside the Person class as this,

public override bool Equals(object o)
        {
            var other = o as Person;
            return this.Name.ToLower().Contains(other.Name.ToLower());
        }

But this is not working. It's not calling this Equal method as well. Can someone help me please.

Thank you.


回答1:


Overriding the equality comparison in this manner is wrong.

Equality should be transitive: if

"FooBar".Equals("Foo") == true

then it must also hold that

"Foo".Equals("FooBar") == true

However, in this case you are using Contains which will break the transitivity because "FooBar" contains "Foo", but "Foo" does not contain "FooBar". Apart from that, you should not override the Equals method on a class unless each and every last comparison between objects of that class will have the same semantics (which in this case seems highly dubious).

So, given that overriding Equals is not the solution, what should you do?

One convenient way is to use LINQ:

var companiesWithPeopleWithLasanthaInTheirName = 
    companies.Where(c => c.persons.Any(p => p.Name.Contains("Lasantha")));

However the above comparison is case-sensitive, so if you need it to not be you have to tweak it; there is an answer in this question: Case insensitive 'Contains(string)'




回答2:


You can use Linq, something like

var temp = companies.Where(p => p.People.Any(q => q.Name.Contains("Lasantha")));

Here is the full example;

public class Example
{
    private IList<Company> companies;
    public Example()
    {
        Person p1 = new Person(){Name = "Lasantha"};
        Person p2 = new Person(){Name = "Red Kid"};
        Company comp = new Company();
        comp.People = new List<Person>();

        comp.People.Add(p1);
        comp.People.Add(p2);

        companies = new List<Company>();
        companies.Add(comp);

        var temp = companies.Where(p => p.People.Any(q => q.Name.Contains("Lasantha")));
    }
}

public class Company
{
    public IList<Person> People
    {
        get;
        set;
    }
}

public class Person
{
    public string Name { get; set; }
}



回答3:


You need to overload Equals so that it takes a Person as parameter. Otherwise it will default to reference comparison.

public override bool Equals(Person p)
{
   //...
}

Then as msdn states, you may need to provide CompareTo (IComparable) as well.




回答4:


I don't think you need to override Equals when you can get this as

you should be using where to filter the companies and then use Any on the Person list to get the ones matching your name criteria

companies.Where(c => c.persons.Any(p => p.Name.Contains("Value here"));



回答5:


You're searching for

  1. a list of characters ("Lasantha")
  2. inside a list of Persons
  3. inside a list of Companies

Well, other contributors already described how to do it correctly



来源:https://stackoverflow.com/questions/9869908/how-to-use-ilist-contains-method-to-find-an-object

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!