c# - 自定义对象的检查列表
问题描述
如果您已经制作了自定义对象列表,如果您想在添加之前检查该列表以查看它是否包含对象,那么必须与哈希码有关,我的意思是这样您就不会在列表中得到重复项或者是有一种更简单的方法,基本上我想在自定义对象列表上使用 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;
}
解决方案
假设您有这样的代码:
List<CustomObject> listOfCustomObjects = new List<CustomObject>();
解决方案 1
如果是这样,您可以使用listOfCustomObjects.Contains(customObject)
找出是否customObject
在listOfCustomObjects
. 您应该添加using System.Linq;
到代码的顶部才能使用此方法。
解决方案 2
另一种在列表中没有重复项的方法基本上是不使用List
. 你可以HashSet
改用。使用此方法,重复的对象不会自动添加到您的列表中。HashSet
也在 LINQ 库中,因此您也应该using System.Linq;
为此解决方案添加行。这是一个如何HashSet
使用您的CustomObject
类创建新的示例:
HashSet<CustomObject> setOfCustomObjects = new HashSet<CustomObject>();
推荐阅读
- html - 如何仅使用 CSS 而不使用 JavaScript 将 CSS 类动态添加到 html 元素
- c# - 不可调用的成员 PictureBox.ImageLocation 不能像方法一样使用
- java - spring-spanner 的写会话保持活动是如何工作的?
- r - ggplot2:如何按填充变量的比例重新排列堆积条形图
- reactjs - 我如何在 NextJs 中访问我的 window.localStorage
- java - 使用嵌套 if 进行验证检查
- flutter - 如何在 Flutter 的 GridView 中设计不同大小的网格?
- python - Python插入/选择排序二维数组
- python - 创建一个表格来显示 python 的平均值
- java - 检查 arrayList 是否包含数组