我正在尝试根据其他一些帖子来解决问题。我正在查看列表中具有相同属性的多个对象,并且需要验证它们在每个对象中都具有相同的值。如果没有,我需要标记对象具有冲突的属性值。我正在尝试并且虽然可以工作的代码在下面,但是即使列表项中的值都相同,也总是返回true以将属性添加到列表中。

public List<string> JobHasConflictingTaxForms(Job job, List<string> propertiesToCheck)
{
    var conflictingPropertiesList = new List<string>();

    foreach(string prop in propertiesToCheck)
    {
         if (!job.TaxForms.TrueForAll(t => t.ARecord.GetType().GetProperty(prop)
                          .GetValue(t.ARecord, null) ==
                           job.TaxForms[0].ARecord.GetType().GetProperty(prop)
                          .GetValue(job.TaxForms[0].ARecord, null)))
         {
             conflictingPropertiesList.Add(prop);
         }
    }

    return conflictingPropertiesList;
}


每个税表都有一个对象,称为“ ARecord”的属性。这些是我需要验证的属性,对于每种税表来说,它们普遍相同,如果没有,那么我需要标记其中的一种区别。我遗漏了什么,或者此lambda语句做错了什么?即使当我具有所有相同的值,或三种具有相同值的形式,而一种形式具有一个属性不同时,它也会返回相同的结果。它总是返回true。

附言我也曾尝试使用Any(i => logic!= other item)。但是没有按照我的意图工作。

TaxForm类

 public class TaxForm
{
    #region Properties

    public string TaxFormType
    {
        get;
        set;
    }

    public string FileName
    {
        get;
        set;
    }

    public string FileFullName
    {
        get;
        set;
    }

    public bool ShouldProcess
    {
        get;
        set;
    }

    public bool Corrected
    {
        get;
        set;
    }

    public TRecord TRecord
    {
        get;
        set;
    }

    public ARecord ARecord
    {
        get;
        set;
    }

    public BRecord BRecord
    {
        get;
        set;
    }

    public List<string> BRecords
    {
        get;
        set;
    }

    public bool HasConflictingrecords
    {
        get;
        set;
    }

    public bool HasDataConflict
    {
        get;
        set;
    }

    #endregion


}

现在是ARecord类

public class ARecord
{
    #region Properties
    /// <summary>
    /// Found at POS(12) for 8
    /// </summary>
    public string PayerTIN
    {
        get;
        set;
    }

    /// <summary>
    /// Found at POS(38) for 39.
    /// </summary>
    public string PayerName
    {
        get;
        set;
    }

    /// <summary>
    /// Found at POS(134) for 39.
    /// </summary>
    public string PayerAddress
    {
        get;
        set;
    }

    /// <summary>
    /// found at POS(255) for 14.
    /// </summary>
    public string PayerPhone
    {
        get;
        set;
    }

    /// <summary>
    /// Found at POS(2) for 3.
    /// </summary>
    public string PayerYear
    {
        get;
        set;
    }

    #endregion


}

最佳答案

PropertyInfo.GetValue方法以类型Object返回它引用的属性的值。执行的==运算符是Object类的一个,而不是String类的一个。

若要执行正确的比较方法,您需要将对PropertyInfo.GetValue的调用的返回值转换为string,然后使用==运算符或调用静态的string.Equals方法。

10-04 16:37
查看更多