C# 如何仅向列表中添加条目<&燃气轮机;当它';名单上还没有什么?
我试图找到一个答案,但似乎找不到任何具体的。我已经找到了关于检查不同条目的回答问题,但我更关心的是一个字段 这是我的代码:C# 如何仅向列表中添加条目<&燃气轮机;当它';名单上还没有什么?,c#,C#,我试图找到一个答案,但似乎找不到任何具体的。我已经找到了关于检查不同条目的回答问题,但我更关心的是一个字段 这是我的代码: PeopleLocationsForUserRoot peoplelocationforuser = await WebDataAccess.GetPeopleLocationForUser(UserInfoRepository.GetUserName(), _locationID); List<LocationPeople>
PeopleLocationsForUserRoot peoplelocationforuser = await WebDataAccess.GetPeopleLocationForUser(UserInfoRepository.GetUserName(), _locationID);
List<LocationPeople> users = new List<LocationPeople>();
foreach (var user in peoplelocationforuser.locationPeople)
{
user.FullName = user.FirstName + " " + user.LastName;
users.Add(user);
}
PeopleLocationsForUserRoot peoplelocationforuser=等待WebDataAccess.GetPeopleLocationForUser(UserInfoRepository.GetUserName(),_locationID);
列表用户=新列表();
foreach(peoplelocationforuser.locationPeople中的var用户)
{
user.FullName=user.FirstName+“”+user.LastName;
用户。添加(用户);
}
在向用户列表中添加条目时,我只想在列表中不存在匹配的全名条目(例如,不能有2个John Smith)的情况下添加该条目。我将从第一个开始:
PeopleLocationsForUserRoot peoplelocationforuser=等待WebDataAccess.GetPeopleLocationForUser(UserInfoRepository.GetUserName(),_locationID);
//创建一个临时字典
var tempDic=新字典();
foreach(peoplelocationforuser.locationPeople中的var用户)
{
user.FullName=user.FirstName+“”+user.LastName;
//添加前请检查字典
如果(!tempDic.ContainsKey(user.FullName))
{
tempDoc.Add(用户);
}
}
//一旦我有了字典,我就可以列一个清单
var用户=新列表(临时值);
如果我理解正确,您需要创建唯一对象的列表。在你的情况下,他们是定位人
在.NET中,唯一值的集合由HashSet类型表示
为了在HashSet中使用,LocationPeople必须实现平等功能。因此,您需要确保LocationPeople确实实施:
var usedNames = new HashSet<string>();
// inside foreach
// Hashset.Add return false if value already in hashset
if(usedNames.Add(fullName))
{
result.Add(peopleObject);
}
var usedNames=new HashSet();
//内foreach
//如果Hashset中已有值,则添加return false
如果(使用名称添加(全名))
{
结果。添加(peopleObject);
}
选项#1
您可以覆盖Equals
方法来比较FullName
属性,并使用Contains
列表的方法:
//overriding the Equals method
public override bool Equals(object obj)
{
User user = obj as User;
if(user == null)
return false;
return this.FullName == user.FullName;
}
//using the Contains method to check if you can add
if(!users.Contains(user))
{
users.Add(user);
}
说明:
Contains
方法使用Equals
方法检查列表是否已具有“equal”对象
因此,覆盖Equals
方法来实现您的业务规则,您可以轻松地使用Contains
方法
无论如何,如果不能仅使用FullName
属性作为Equals
实现,则应在添加前使用foreach
语句比较列表中的每个条目,比较FullName
属性
选项2 您可以使用
HashSet
将唯一值保存到“列表”(现在是HashSet)中。
无论如何,您应该重写GetHashCode
方法,通过FullName
属性(仅通过FullName
属性)获取HashCode,只需将您user
实例添加到users
HashMap
//overriding the GetHashCode method
public override int GetHashCode()
{
return this.FullName.GetHashCode();
}
//Add
HashSet<LocationPeople> users = new HashSet<LocationPeople>();
users.Add(user);
//重写GetHashCode方法
公共覆盖int GetHashCode()
{
返回这个.FullName.GetHashCode();
}
//加
HashSet users=新HashSet();
用户。添加(用户);
解释 HashSet使用HashCode来标识条目。因此,如果您得到两个具有相同HashCode的对象(基于
GetHashCode
实现的相同对象),当您添加此相同对象时,CLR会知道这一点,并删除旧对象,并在旧对象所在的相同位置添加新对象
如果列表中不存在匹配的全名条目=>
如果(!users.Any(u=>u.FullName==user.FullName))
那么我建议您只需检查“是否存在匹配的全名条目”在您添加它之前。@ManfredRadlwimmer我基本上是在问如何做。@connersz您(或至少您的代码建议)对每个循环和列表都很熟悉。是什么阻止您检查列表是否包含满足特定条件的条目(if(x.FullName==xy)
)?你为什么不直接做呢?Ivan的Linq查询是最简单的方法,但我不明白为什么你自己不能弄清楚。缺少哪一步?你试过什么?你确定你想要一个列表
?您的要求听起来更像一套。更改整个类型非常好,而且几乎是一个非常突破性的更改,这取决于OPs程序的其余部分。@Equalsk breaking?我在方法的末尾创建OP List对象。怎么会坏呢?哦,对不起,很明显我的咖啡没喝进去。我要回去睡觉了。我可能会建议使用HashSet
,但使用一个接受IEqualityComparer
的构造函数,这样就不必对现有类型中的相等性做出全局决定。(另外,请注意,如果要做出此全局决策,则必须同时覆盖GetHashCode
和Equals
)
var usedNames = new HashSet<string>();
// inside foreach
// Hashset.Add return false if value already in hashset
if(usedNames.Add(fullName))
{
result.Add(peopleObject);
}
//overriding the Equals method
public override bool Equals(object obj)
{
User user = obj as User;
if(user == null)
return false;
return this.FullName == user.FullName;
}
//using the Contains method to check if you can add
if(!users.Contains(user))
{
users.Add(user);
}
//overriding the GetHashCode method
public override int GetHashCode()
{
return this.FullName.GetHashCode();
}
//Add
HashSet<LocationPeople> users = new HashSet<LocationPeople>();
users.Add(user);