C# 如何在类型类的字典中使用ContainsValue
我正在创建类的字典,但我不能使用“ContainsValue”方法。请帮忙 我的班级:C# 如何在类型类的字典中使用ContainsValue,c#,dictionary,C#,Dictionary,我正在创建类的字典,但我不能使用“ContainsValue”方法。请帮忙 我的班级: class clsAttendance { public string TagId { get; set; } public DateTime LastFoundDate { get; set; } public DateTime CapturedAt { get; set; } } 我的字典: private Dictionary<
class clsAttendance
{
public string TagId { get; set; }
public DateTime LastFoundDate { get; set; }
public DateTime CapturedAt { get; set; }
}
我的字典:
private Dictionary<int, clsAttendance> dicAttendance = new Dictionary<int, clsAttendance>();
你可以用
bool rfidExists = dicAttendance.Values.Any(a => a.TagId.Equals("123456AANN"));
你可以用
bool rfidExists = dicAttendance.Values.Any(a => a.TagId.Equals("123456AANN"));
您的类
clsatendance
必须重写Equals
或实现IEquatable
。否则,只有当它是相同的引用时才能找到它。始终也覆盖GethashCode
此示例显示了建议使用的两种方法:
public class clsAttendance : IEquatable<clsAttendance>
{
public string TagId { get; set; }
public DateTime LastFoundDate { get; set; }
public DateTime CapturedAt { get; set; }
public override bool Equals(object obj)
{
var otherAttendance = obj as clsAttendance;
if (otherAttendance == null)
{
return false;
}
return this.Equals(otherAttendance);
}
public override int GetHashCode()
{
return (this.TagId != null ? this.TagId.GetHashCode() : 0);
}
public bool Equals(clsAttendance other)
{
if (ReferenceEquals(null, other)) return false;
if (ReferenceEquals(this, other)) return true;
return string.Equals(this.TagId, other.TagId);
}
}
public class clsatendance:IEquatable
{
公共字符串TagId{get;set;}
公共日期时间LastFoundDate{get;set;}
public DateTime CapturedAt{get;set;}
公共覆盖布尔等于(对象对象对象)
{
var OtherAttention=作为CLSATentance的obj;
if(otherAttention==null)
{
返回false;
}
返回此。等于(其他出席人数);
}
公共覆盖int GetHashCode()
{
返回(this.TagId!=null?this.TagId.GetHashCode():0);
}
公共布尔等于(clsAttendance其他)
{
if(ReferenceEquals(null,other))返回false;
if(ReferenceEquals(this,other))返回true;
返回字符串.Equals(this.TagId,other.TagId);
}
}
您的类clsatendance
必须重写等于
或实现IEquatable
。否则,只有当它是相同的引用时才能找到它。始终也覆盖GethashCode
此示例显示了建议使用的两种方法:
public class clsAttendance : IEquatable<clsAttendance>
{
public string TagId { get; set; }
public DateTime LastFoundDate { get; set; }
public DateTime CapturedAt { get; set; }
public override bool Equals(object obj)
{
var otherAttendance = obj as clsAttendance;
if (otherAttendance == null)
{
return false;
}
return this.Equals(otherAttendance);
}
public override int GetHashCode()
{
return (this.TagId != null ? this.TagId.GetHashCode() : 0);
}
public bool Equals(clsAttendance other)
{
if (ReferenceEquals(null, other)) return false;
if (ReferenceEquals(this, other)) return true;
return string.Equals(this.TagId, other.TagId);
}
}
public class clsatendance:IEquatable
{
公共字符串TagId{get;set;}
公共日期时间LastFoundDate{get;set;}
public DateTime CapturedAt{get;set;}
公共覆盖布尔等于(对象对象对象)
{
var OtherAttention=作为CLSATentance的obj;
if(otherAttention==null)
{
返回false;
}
返回此。等于(其他出席人数);
}
公共覆盖int GetHashCode()
{
返回(this.TagId!=null?this.TagId.GetHashCode():0);
}
公共布尔等于(clsAttendance其他)
{
if(ReferenceEquals(null,other))返回false;
if(ReferenceEquals(this,other))返回true;
返回字符串.Equals(this.TagId,other.TagId);
}
}
没有使用LINQ的任何其他方法?是的,请参阅TimSchmelter的答案:-)foreach(indicatendance.Values中的var a){if(a.TagId.Equals(“123456AANN”){return true;}}return false代码>没有使用LINQ的任何其他方法?是的,请参阅TimSchmelter的答案:-)foreach(indicatendance.Values中的var a){if(a.TagId.Equals(“123456AANN”){return true;}}return false代码>检查值是否存在时如何使用此选项。。。我需要调用什么方法?icAttendance.ContainsValue(clsAtt)代码>检查值是否存在时如何使用此选项。。。我需要调用什么方法?icAttendance.ContainsValue(clsAtt)代码>