2

大家早上好,

我正在尝试使用“包含”来查看对象是否在集合中。当我休息时,我可以看到该对象确实是集合的一部分,但是“包含”似乎返回 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”需要为空。有任何想法吗?

***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;
            }
        }
    }
}

谢谢,~ck 在圣地亚哥

4

5 回答 5

3

编辑:正如艾哈迈德指出的那样,您的条件运算符用法不正确。但是,您甚至不需要在此处使用条件运算符,因为其中一个分支会导致无操作。只需使用这个:

if (autopays != null && payType != null && !autopays.Contains(payType))
{
    paymentAdd.DaysPaid = null;
}

原始答案

你还没有展示任何关于PaymentTypeInfo- 它是否覆盖EqualsGetHashCode适当?如果不是,则将使用引用标识执行包含检查,并且会话中的引用与存储库中的引用不太可能相同。

要么PaymentTypeInfo覆盖EqualsGetHashCode,要么将适当IEqualityComparer<PaymentTypeInfo>的传递给Contains方法。

(正如 SLaks 在评论中提到的那样,在这种情况下GetHashCode实际上不会被调用 - 但你应该始终覆盖它们EqualsGetHashCode或者都不覆盖它们;如果你确实覆盖它们,你应该以一致的方式这样做。)

于 2010-03-18T17:45:58.387 回答
2

除非payType覆盖Equals或您指定IEqualityComparer,Contains将通过引用进行比较。

您的集合可能包含逻辑上等效的类的不同实例。

于 2010-03-18T17:46:04.223 回答
1

您可能会遇到相等问题 - Contains() 使用 IEquatable.Equals 方法,因此您可能会检查以确保该方法将为 PaymentTypeInfo 类的单独实例返回 true。

于 2010-03-18T17:45:56.047 回答
1

到目前为止,每个帖子都有一个有效的观点;取决于所使用的类型Contains可能不够。不过,我正在解决您问题的另一部分:

如果对象不在集合中,则“DaysPaid”需要为空。有任何想法吗?

如何切换三元运算符值的顺序以匹配上述语句?用这个:

paymentAdd.DaysPaid = autopays.Contains(payType) ? paymentAdd.DaysPaid : null;

而不是这个:

paymentAdd.DaysPaid = autopays.Contains(payType) ? null : paymentAdd.DaysPaid;

如果语句评估为false第二个项目将被使用,所以 make it null。结构是:

logic statement ? true : false
于 2010-03-18T17:48:12.877 回答
0

可以发一下PaymentType课程的来源吗?我相当确定这种类型不提供基于值的语义,因此该Contains方法被迫求助于使用身份相等(这不会给您想要的结果)。

如果是这种情况,您可能会对我写的关于这个主题的这些文章感兴趣:

于 2010-03-18T17:46:20.763 回答