简体   繁体   English

搜索自定义类列表的属性

[英]Searching the properties of a list of a custom class

I have a custom class called CustomClass. 我有一个名为CustomClass的自定义类。 It contains a variable called "Name" and a list of values (for the sake of simplicity let's make this an int - in reality it is another custom class, but the principle should be the same). 它包含一个名为“ Name”的变量和一个值列表(为简单起见,我们将其设置为int -实际上,它是另一个自定义类,但原理应相同)。

So : 因此:

public class CustomClass {
   string name;


} 

I have a List<CustomClass> . 我有一个List<CustomClass>

When I attempt to add a value to this List, the logic I want, is for this List to check if it contains a CustomClass with the same name as the value I want to add. 当我尝试向此列表添加值时,我想要的逻辑是让此列表检查它是否包含与我要添加的值同名的CustomClass。

If it does, then do x, otherwise, do y. 如果是,则执行x,否则执行y。

listOfCustomClass.Contains(customClassToAdd.name) will not work in this case, I assume, however this is the functionality I require. 我认为listOfCustomClass.Contains(customClassToAdd.name)在这种情况下不起作用,但这是我需要的功能。

What is best practice here ? 什么是最佳做法?

我认为您可以尝试类似var x = MyList.Where(C => C.Name == InsertedName)的方法并检查结果(未经测试)

You'll have to create a new class,let's call it CustomList, that inherits from IList<> where you can override the add method, do your check, and then add it to the base. 您必须创建一个新类,将其称为CustomList,该类继承自IList <>,您可以在其中重写add方法,进行检查,然后将其添加到基础中。 Something like this: 像这样:

public class CustomList<T> : IList<T> where T : CustomClass
{
    private List<T> innerlist;
    public void Add(T item)
    {
        if(innerlist.Any(a => a.Name == item.Name)))
            innerlist.Add(item);
    }
}

you can do it using linq as follow but you have to make name field public. 您可以按照以下步骤使用linq进行操作,但必须将名称字段设为公开。

        List<CustomClass> list = new List<CustomClass>();
        CustomClass toCheck = new CustomClass();

        if (list.Any(p => p.name.Equals(toCheck)))
        {
            //do x here
        }
        else
        {
            //do y here
        }

however if you don't want to use linq then Do some changes in CustomClass as follow 但是,如果您不想使用linq,请在CustomClass中进行如下更改

public class CustomClass
{
    string name;
    List<int> intLost = new List<int>();

    public override bool Equals(object obj)
    {
        return this.Equals(obj as CustomClass);
    }

    public override int GetHashCode()
    {
        return 0;
    }

    public bool Equals(CustomClass cc)
    {
        if (cc == null) return false;
        return this.name.Equals(cc.name);
    }
}

Then you can do this. 然后,您可以执行此操作。

        List<CustomClass> list = new List<CustomClass>();

        CustomClass toCheck = new CustomClass();
        if (list.Contains(toCheck))
        {
            //do x here
        }
        else
        {
            //do y here
        }

It seems to me that you want to override the .Add() behavior of your List<CustomClass>. 在我看来,您想覆盖List <CustomClass>的.Add()行为。 While you could use extension methods, I think a better solution would be to invent a class that extends List in some manner. 尽管可以使用扩展方法,但我认为更好的解决方案是发明一个以某种方式扩展List的类。 I'd recommend implementing IList in your collection class if you need to have that level of control over add operations... 如果需要对添加操作进行控制,我建议在您的集合类中实现IList。

    public class CustomClassList : IList<CustomClass>
    {
        public void Add (CustomClass item)
        {
            if(this.Select(t => t.Name).Contains(item.Name))
                // Do whatever here...
            else
                // Do whatever else here...
        }

        // ... other IList implementations here ...
    }

try this: 尝试这个:

IList<CustomClass> list = new List<CustomClass>();

CustomClass customClass = new CustomClass();
customClass.name = "Lucas";

if((list.Tolist().Find(x => x.name == customClass.name)) == null)
{
    list.Add(customClass);
}
else
{
    //do y;
}

You could override the Equals(object o) function in your CustomClass, so that two CustomClasses are considered equal if their names are the same. 您可以在CustomClass中重写Equals(object o)函数,以便如果两个CustomClass的名称相同,则认为它们相等。 Then 然后

listOfCustomClass.Contains(customClassToAdd);

should work. 应该管用。

另一种方法是在CustomClass上重写Equals方法,然后仅调用List.Contains()

If the name property uniquely identifies the CustomClass, then you should overload Equals and GetHashCode() . 如果name属性唯一标识CustomClass,则应重载EqualsGetHashCode() The reason List.Contains doesn't work is that underneath the HashCodes are compared. List.Contains不起作用的原因是在HashCodes下方进行了比较。 So you need to overload GetHashCode and Equals something like this: 因此,您需要重载GetHashCodeEquals如下所示:

public override int GetHashCode()
{
    return this.name.GetHashCode();
}

public override bool Equals(object obj)
{
    var other = obj as CustomClass;

    if (other != null)
    {
        if (other.Name == this.Name)
        {
             return true;
        }
    }
    return false;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM