Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 搜索自定义类列表的属性_C#_List_If Statement_Iteration_Distinct - Fatal编程技术网

C# 搜索自定义类列表的属性

C# 搜索自定义类列表的属性,c#,list,if-statement,iteration,distinct,C#,List,If Statement,Iteration,Distinct,我有一个名为CustomClass的自定义类。它包含一个名为Name的变量和一个值列表,为了简单起见,让我们将其设为int-实际上它是另一个自定义类,但原理应该是相同的 因此: 我有一张单子 当我尝试向该列表添加值时,我想要的逻辑是该列表检查它是否包含与我想要添加的值同名的CustomClass 如果是,则执行x,否则执行y 我假设listOfCustomClass.ContainesCustomClassToAdd.name在这种情况下不起作用,但这是我需要的功能 这里的最佳实践是什么?我认为

我有一个名为CustomClass的自定义类。它包含一个名为Name的变量和一个值列表,为了简单起见,让我们将其设为int-实际上它是另一个自定义类,但原理应该是相同的

因此:

我有一张单子

当我尝试向该列表添加值时,我想要的逻辑是该列表检查它是否包含与我想要添加的值同名的CustomClass

如果是,则执行x,否则执行y

我假设listOfCustomClass.ContainesCustomClassToAdd.name在这种情况下不起作用,但这是我需要的功能


这里的最佳实践是什么?

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

您可以覆盖CustomClass中的Equalsobject o函数,这样,如果两个CustomClass的名称相同,则认为它们是相等的。然后

listOfCustomClass.Contains(customClassToAdd);

应该可以工作。

您必须创建一个新类,我们称之为CustomList,它继承自IList,您可以重写add方法,进行检查,然后将其添加到基。大概是这样的:

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);
    }
}
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;
}

另一种方法是重写CustomClass上的Equals方法,然后只调用List。包含

如果name属性唯一标识CustomClass,则应重载Equals和GetHashCode。原因列表.Contains不起作用是哈希代码下面有比较。因此,您需要重载GetHashCode并等于以下内容:

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);
    }
}
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;
}

您可以使用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
        }
但是,如果您不想使用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);
    }
}
那你就可以这么做了

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

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

在我看来,您希望覆盖列表的.Add行为。虽然您可以使用扩展方法,但我认为更好的解决方案是发明一个以某种方式扩展列表的类。如果您需要对添加操作进行那种级别的控制,我建议您在集合类中实现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 ...
    }
试试这个:

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;
}

固定的谢谢你指出。你为什么要写名字?不是listOfCustomClass.ContainesCustomClassToAddNo,这就是重点。我想检查它是否包含同名的自定义类:请更正它List LISTOFINGERS=new List;您丢失了。我将删除它,因为它完全不相关:您可以忽略IList包装器,将注意力集中在Any语句上。在编辑之前,我编写了IList包装,澄清了您的问题。最好使用Any而不是Where,因为Simon只是查看集合是否包含匹配项,而不关心检索所有匹配项。尝试此操作时,出现错误5无法隐式将类型“CustomClass”转换为bool。我的代码是:var classExists=listOfCustomClass.Wherec=>c.Name==classToAdd.Name;发布前回答的问题!:请小心,classExists不是布尔值,它的类型是list,您应该测试它是否为null或没有元素,如果它的元素计数>0,则名称已经存在。