首页 > 解决方案 > 自定义对象的检查列表

问题描述

如果您已经制作了自定义对象列表,如果您想在添加之前检查该列表以查看它是否包含对象,那么必须与哈希码有关,我的意思是这样您就不会在列表中得到重复项或者是有一种更简单的方法,基本上我想在自定义对象列表上使用 contains 方法来查看我要添加的对象是否已经存在于列表中,如果有更简单的方法那么必须处理哈希码?

这是我的代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DataConverter.Objects;

namespace DataConverter.Converters
{
    class CategoryConverter
    {
        private Category category;
        private SubCategory subCategory;
        private ExcellObj excellObj; 
         


        public CategoryConverter(string path)
        {
            excellObj = new ExcellObj(path); 
        }

        public List<Category> getCategoryListExcel()
        {
            List<Category> categories = new List<Category>();

            List<string> ColumnNames = new List<string> { "Group1", "Group1Descr" };
            List<int> CorrectColumn = new List<int>(); 


            for(int i = 0; i < ColumnNames.Count; i++)
            {
                CorrectColumn.Add(excellObj.findColumn(ColumnNames[i]));
            }

            
            for(int i = 2; i < excellObj.allRows; i++)
            {   
              
                   
                
                    categories.Add(category = new Category(excellObj.getValuesFromCell(i, CorrectColumn[1]), excellObj.getValuesFromCell(i, CorrectColumn[0]), "Home"));
                
              
            }


            





            return categories; 


        }
        public List<List<SubCategory>> getSubCategory()
        {
            List<SubCategory> subCategories1 = new List<SubCategory>();
            List<SubCategory> subCategories2 = new List<SubCategory>();
            List<List<SubCategory>> subCategoriesList = new List<List<SubCategory>>(); 
            List<string> ColumnNamesSubCategory1 = new List<string> { "Group2", "Group2Descr" };
            List<string> ColumnNamesSubCategory2 = new List<string> { "Group3", "Group3Desc" };
            List<int> CorrectColumn1 = new List<int>();
            List<int> CorrectColumn2 = new List<int>();


            for(int i = 0; i < ColumnNamesSubCategory1.Count; i++)
            {
                CorrectColumn1.Add(excellObj.findColumn(ColumnNamesSubCategory1[i]));
                CorrectColumn2.Add(excellObj.findColumn(ColumnNamesSubCategory2[i]));
            }

            for(int i = 1; i < excellObj.allRows; i++)
            {
                subCategories1.Add(subCategory = new SubCategory(excellObj.getValuesFromCell(i, CorrectColumn1[1]),excellObj.getValuesFromCell(i,CorrectColumn1[0]), "Home"));
                subCategories2.Add(subCategory = new SubCategory(excellObj.getValuesFromCell(i,CorrectColumn2[1]), excellObj.getValuesFromCell(i,CorrectColumn2[0]), "Home"));
            }

            subCategoriesList.Add(subCategories1);
            subCategoriesList.Add(subCategories2);


            return subCategoriesList;
        }

        public void finnishedUsingExcel()
        {
            excellObj.CloseApplication();
        }

    }
}

而我想要发生的是我想要运行一个

if(categories.Contains(category) == false){
    categories.add(category)
}

我不明白文档中的这一部分?

   public Person(string lastName, string ssn)
   {
      if (Regex.IsMatch(ssn, @"\d{9}"))
        uniqueSsn = $"{ssn.Substring(0, 3)}-{ssn.Substring(3, 2)}-{ssn.Substring(5, 4)}";
      else if (Regex.IsMatch(ssn, @"\d{3}-\d{2}-\d{4}"))
         uniqueSsn = ssn;
      else
         throw new FormatException("The social security number has an invalid format.");

      this.LastName = lastName;
   }

标签: c#listcontains

解决方案


假设您有这样的代码:

List<CustomObject> listOfCustomObjects = new List<CustomObject>();

解决方案 1

如果是这样,您可以使用listOfCustomObjects.Contains(customObject)找出是否customObjectlistOfCustomObjects. 您应该添加using System.Linq;到代码的顶部才能使用此方法。

解决方案 2

另一种在列表中没有重复项的方法基本上是不使用List. 你可以HashSet改用。使用此方法,重复的对象不会自动添加到您的列表中。HashSet也在 LINQ 库中,因此您也应该using System.Linq;为此解决方案添加行。这是一个如何HashSet使用您的CustomObject类创建新的示例:

HashSet<CustomObject> setOfCustomObjects = new HashSet<CustomObject>();

推荐阅读