【问题标题】:Searching the properties of a list of a custom class搜索自定义类列表的属性
【发布时间】:2012-05-17 14:06:38
【问题描述】:

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

所以:

public class CustomClass {
   string name;


} 

我有一个List<CustomClass>

当我尝试向这个 List 添加一个值时,我想要的逻辑是让这个 List 检查它是否包含一个与我要添加的值同名的 CustomClass。

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

我认为

listOfCustomClass.Contains(customClassToAdd.name) 在这种情况下不起作用,但这是我需要的功能。

这里的最佳做法是什么?

【问题讨论】:

  • 已修复。感谢您指出。
  • 你为什么要放.name?不会是 listOfCustomClass.Contains(customClassToAdd)
  • 不,这就是重点。我想检查它是否包含同名的自定义类:)
  • 请更正List<int> listOfIntegers = new List<int>();你错过了()
  • 我将删除它,因为它完全无关紧要:)

标签: c# list if-statement iteration distinct


【解决方案1】:

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

【讨论】:

  • 也可以使用 Any 而不是 Where,因为 Simon 只是查看集合是否包含匹配条目,而不关心检索所有匹配条目。
  • 当我尝试此操作时,我收到错误错误 5 无法将类型“CustomClass”隐式转换为布尔值。我的代码是: var classExists = listOfCustomClass.Where(c => c.Name == classToAdd.Name);
  • 问题发布前已回答! :)
  • 小心,classExists 不是布尔值,它的类型是 list,你应该测试它是 null 还是没有元素,如果它有元素(count> 0)那么这个名字已经存在了。跨度>
【解决方案2】:

您必须创建一个新类,我们称之为 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);
    }
}

【讨论】:

  • 您可以忽略 IList 包装器并专注于 Any() 语句。在您澄清您的问题之前,我编写了 IList 包装器。
【解决方案3】:

您可以使用 linq 进行如下操作,但您必须公开 name 字段。

        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
        }

【讨论】:

    【解决方案4】:

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

    【讨论】:

      【解决方案5】:

      试试这个:

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

      【讨论】:

        【解决方案6】:

        您可以在 CustomClass 中覆盖 Equals(object o) 函数,这样如果两个 CustomClass 的名称相同,则它们被视为相等。那么

        listOfCustomClass.Contains(customClassToAdd);
        

        应该可以。

        【讨论】:

          【解决方案7】:

          另一种方法是覆盖 CustomClass 上的 Equals 方法,然后调用 List.Contains()

          【讨论】:

            【解决方案8】:

            如果name 属性唯一标识了CustomClass,那么您应该重载EqualsGetHashCode()List.Contains 不起作用的原因是在 HashCodes 下面进行了比较。所以你需要重载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;
            }
            

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2020-08-26
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多