0

リストを互いに比較する方法について読んでいます。IEquatableインターフェイスを実装しようとしました。これが私がこれまでに行ったことです:

/// <summary>
/// A object holder that contains a service and its current failcount
/// </summary>
public class ServiceHolder : IEquatable<ServiceHolder>
{
    /// <summary>
    /// Constructor
    /// </summary>
    /// <param name="service"></param>
    public ServiceHolder(Service service)
    {
        Service = service;
        CurrentFailCount = 0;
    }
    public Service Service { get; set; }
    public UInt16 CurrentFailCount { get; set; }


    /// <summary>
    /// Public equal method
    /// </summary>
    /// <param name="obj"></param>
    /// <returns></returns>
    public override bool Equals(object obj)
    {
        if (obj == null)
        {
            return false;
        }

        ServiceHolder tmp = obj as ServiceHolder;
        if (tmp == null)
        {
            return false;
        }
        else
        {
            return Equals(tmp);
        }
    }

    /// <summary>
    /// Checks the internal components compared to one annother
    /// </summary>
    /// <param name="serviceHolder"></param>
    /// <returns>tru eif they are the same else false</returns>
    public bool Equals(ServiceHolder serviceHolder)
    {
        if (serviceHolder == null)
        {
            return false;
        }

        if (this.Service.Id == serviceHolder.Service.Id)
        {
            if (this.Service.IpAddress == serviceHolder.Service.IpAddress)
            {
                if (this.Service.Port == serviceHolder.Service.Port)
                {
                    if (this.Service.PollInterval == serviceHolder.Service.PollInterval)
                    {
                        if (this.Service.ServiceType == serviceHolder.Service.ServiceType)
                        {
                            if (this.Service.Location == serviceHolder.Service.Location)
                            {
                                if (this.Service.Name == this.Service.Name)
                                {
                                    return true;
                                }
                            }
                        }
                    }
                }
            }
        }
        return false;
    }
}

これが私がそれを使用する場所です:

 private void CheckIfServicesHaveChangedEvent()
 {
     IList<ServiceHolder> tmp;
     using (var db = new EFServiceRepository())
     {
         tmp = GetServiceHolders(db.GetAll());
     }

     if (tmp.Equals(Services))
     {
         StateChanged = true;
     }
     else
     {
         StateChanged = false;
     }
 }

デバッグして equals 関数にブレークポイントを設定すると、ヒットすることはありません。

これは、私がそれを間違って実装したか、それとも正しく呼び出していないと思いますか?

4

2 に答える 2

3

2 つのリストの内容を比較する場合、最適な方法はSequenceEqualです。

if (tmp.SequenceEquals(Services))

これにより、リスト内の値に対する等価セマンティクスを使用して、両方のリストの内容が比較されます。この場合、要素の型はServiceHolderであり、この型の等価セマンティクスを既に定義しているため、問題なく動作するはずです

編集

OPは、コレクションの順序は重要ではないとコメントしました。そのシナリオでは、次のことができます

if (!tmp.Except(Services).Any()) 
于 2014-02-21T10:46:50.207 に答える
0

linq を使用すると、順序のないリストを最も簡単に比較できます。

 List<ServiceHolder> result = tmp.Except(Services).ToList();
于 2014-02-21T10:52:44.043 に答える