早安全,
我正在尝试使用“包含”来查看对象是否在集合中。当我打破时,我可以看到该对象确实是集合的一部分,但是“Contains”似乎返回false表示该项目不在集合中。知道我做错了吗?
if(HttpContext.Current.Session["AutoPayTypes"] != null)
{
var autopays = HttpContext.Current.Session["AutoPayTypes"] as List<PaymentTypeInfo>;
char? coverageProductLine = null;
if(entityProps.ContainsKey("CoverageProductLine"))
{
coverageProductLine = (char?)entityProps["CoverageProductLine"];
}
var paymentTypeInfoRepository = new PaymentTypeInfoRepository();
var payType = paymentTypeInfoRepository.GetPaymentTypeInfo(paymentAdd.PayType,
coverageProductLine);
if (autopays != null && payType != null)
paymentAdd.DaysPaid = autopays.Contains(payType) ? null : paymentAdd.DaysPaid;
}
如果对象不在集合中,则“DaysPaid”必须为null。有什么想法吗?
*** UPDATE PaymentTypeInfo是标准的LinqToSql生成的类。此时已覆盖Equals和GetHashCode。这是它的来源。
[Table(Name="dbo.S_OptPaymentType")]
public partial class PaymentTypeInfo
{
private string _PaymentId;
private string _PaymentCode;
private System.Nullable<char> _CoverageType;
private string _ActionCode;
private System.Nullable<char> _PaymentType;
private string _BenAction;
private System.Nullable<char> _BenPremDisFlag;
private string _APNextToLastAct;
private string _APLastAct;
public PaymentTypeInfo()
{
}
[Column(Storage="_PaymentId", DbType="Char(3) NOT NULL", CanBeNull=false)]
public string PaymentId
{
get
{
return this._PaymentId;
}
set
{
if ((this._PaymentId != value))
{
this._PaymentId = value;
}
}
}
[Column(Storage="_PaymentCode", DbType="Char(2) NOT NULL", CanBeNull=false)]
public string PaymentCode
{
get
{
return this._PaymentCode;
}
set
{
if ((this._PaymentCode != value))
{
this._PaymentCode = value;
}
}
}
[Column(Storage="_CoverageType", DbType="Char(1)")]
public System.Nullable<char> CoverageType
{
get
{
return this._CoverageType;
}
set
{
if ((this._CoverageType != value))
{
this._CoverageType = value;
}
}
}
[Column(Storage="_ActionCode", DbType="VarChar(3)")]
public string ActionCode
{
get
{
return this._ActionCode;
}
set
{
if ((this._ActionCode != value))
{
this._ActionCode = value;
}
}
}
[Column(Name="PaymentType", Storage="_PaymentType", DbType="Char(1)")]
public System.Nullable<char> PaymentType
{
get
{
return this._PaymentType;
}
set
{
if ((this._PaymentType != value))
{
this._PaymentType = value;
}
}
}
[Column(Storage="_BenAction", DbType="VarChar(3)")]
public string BenAction
{
get
{
return this._BenAction;
}
set
{
if ((this._BenAction != value))
{
this._BenAction = value;
}
}
}
[Column(Storage="_BenPremDisFlag", DbType="Char(1)")]
public System.Nullable<char> BenPremDisFlag
{
get
{
return this._BenPremDisFlag;
}
set
{
if ((this._BenPremDisFlag != value))
{
this._BenPremDisFlag = value;
}
}
}
[Column(Storage="_APNextToLastAct", DbType="VarChar(3)")]
public string APNextToLastAct
{
get
{
return this._APNextToLastAct;
}
set
{
if ((this._APNextToLastAct != value))
{
this._APNextToLastAct = value;
}
}
}
[Column(Storage="_APLastAct", DbType="VarChar(3)")]
public string APLastAct
{
get
{
return this._APLastAct;
}
set
{
if ((this._APLastAct != value))
{
this._APLastAct = value;
}
}
}
}
谢谢, 〜在圣地亚哥
答案 0 :(得分:3)
if (autopays != null && payType != null && !autopays.Contains(payType))
{
paymentAdd.DaysPaid = null;
}
原始回答
您没有展示PaymentTypeInfo
的任何内容 - 是否会恰当地覆盖Equals
和GetHashCode
?如果没有,将使用引用标识执行包含检查,并且会话中的引用与存储库中的引用相同。
要么PaymentTypeInfo
覆盖Equals
和GetHashCode
,要么将适当的IEqualityComparer<PaymentTypeInfo>
传递给Contains
方法。
(正如SLaks在评论中提到的那样,在这种情况下,GetHashCode
实际上不会被调用 - 但是您应该始终覆盖Equals
和GetHashCode
,或者都不覆盖它们;如果您做覆盖它们,你应该以一致的方式这样做。)
答案 1 :(得分:2)
除非payType
覆盖Equals
或您指定IEqualityComparer
,否则Contains
将通过引用进行比较。
您的集合可能包含一个逻辑等效的类的不同实例。
答案 2 :(得分:1)
您可能遇到了相同的问题 - Contains()使用IEquatable.Equals方法,因此您可以检查以确保对于PaymentTypeInfo类的单独实例,它将返回true。
答案 3 :(得分:1)
到目前为止,每篇文章都有一个有效的观点;根据使用的类型Contains
可能还不够。我在解决你问题的另一部分:
如果对象不在集合中 “DaysPaid”必须为null。任何 想法?
如何切换三元运算符值的顺序以匹配上述语句?使用此:
paymentAdd.DaysPaid = autopays.Contains(payType) ? paymentAdd.DaysPaid : null;
而不是:
paymentAdd.DaysPaid = autopays.Contains(payType) ? null : paymentAdd.DaysPaid;
如果语句评估为false
,则将使用第二项,因此将其设为null
。结构是:
logic statement ? true : false
答案 4 :(得分:0)
您可以发布PaymentType
课程的来源吗?我很确定这种类型没有提供基于值的语义,所以Contains
方法被迫使用身份相等(这不会给你你想要的结果)。
如果是这种情况,您可能会对我在这个主题上撰写的这些文章感兴趣: