簡體   English   中英

不使用Linq比較2 List <>

[英]Compare 2 List<> without using Linq

我正在使用2 List,我想看看主要包含相同的類型。 這兩個列表不需要包含相同的計數或順序,只需具有所有匹配的類型。 我知道使用Linq很有可能,但是我不能使用它。

    private static bool ContentsMatch(List<Type> list1, List<Type> list2)
    {
        if (list1.Count != list2.Count)
            return false;

        for (int i = 0; i < list1.Count; i++)
        {
            if (!list1[i].Equals(list2[i]))
                return false;
        }
        return true;
    }

我嘗試過的上述方法僅在順序相同時才返回true。

注釋中提供的算法代碼。

不依賴於訂單或計數或重復項。 也是通用和抽象的。

bool IsSameSet<T>(IEnumerable<T> l1, IEnumerable<T> l2)
{
  return IsSubSet(l1, l2) && IsSubSet(l2, l1); 
}

bool IsSubSet<T>(IEnumerable<T> l1, IEnumerable<T> l2)
{
  var lookup = new Dictionary<T, bool>();

  foreach (var e in l1)
    lookup[e] = true;

  foreach (var e in l2)
    if (!lookup.ContainsKey(e))
      return false;

  return true;
}

用法:

Type[] l1 = { typeof(object), typeof(int), typeof(long), typeof(object) };
Type[] l2 = { typeof(int), typeof(long), typeof(object) };

var result = IsSameSet(l1, l2);
Console.WriteLine(result);  // prints true

用戶練習:

添加一個附加參數以提供IEqualityComparer<T>傳遞給字典。

要比較任何用戶定義的自定義類型,我們需要重寫Equals和GetHashCode。 以下是您可以參考的代碼段:

    public class CustomizedDataType
    {
        private int field1;
        private string field2;

        public CustomizedDataType(int field1,string field2)
        {
            this.field1 = field1;
            this.field2 = field2;
        }

        public override bool Equals(object obj)
        {
            CustomizedDataType dataType = obj as CustomizedDataType;
            if (this.field1 == dataType.field1 && this.field2 == dataType.field2)
            {
                return true;
            }
            return false;
        }

        public override int GetHashCode()
        {
            return (this.field1.GetHashCode() + this.field2.GetHashCode());
        }

要執行的示例代碼:

    static void Main(string[] args)
    {
        //Test Data
        List<CustomizedDataType> dataTypeContaineer1 = new List<CustomizedDataType>();
        dataTypeContaineer1.Add(new CustomizedDataType(10,"Test10"));
        dataTypeContaineer1.Add(new CustomizedDataType(11, "Test11"));
        dataTypeContaineer1.Add(new CustomizedDataType(12, "Test12"));

        //Test Data
        List<CustomizedDataType> dataTypeContaineer2 = new List<CustomizedDataType>();
        dataTypeContaineer2.Add(new CustomizedDataType(100, "Test10"));
        dataTypeContaineer2.Add(new CustomizedDataType(11, "Test11"));
        dataTypeContaineer2.Add(new CustomizedDataType(12, "Test120"));

        //Checking if both the list contains the same types.
        if (dataTypeContaineer1.GetType() == dataTypeContaineer2.GetType())
        {
            //Checking if both the list contains the same count
            if (dataTypeContaineer1.Count == dataTypeContaineer2.Count)
            {
                //Checking if both the list contains the same data.
                for (int index = 0; index < dataTypeContaineer1.Count; index++)
                {
                    if(!dataTypeContaineer1[index].Equals(dataTypeContaineer2[index]))
                    {
                        Console.WriteLine("Mismatch @ Index {0}", index);
                    }
                }
            }
        }
    }

輸出:

在此處輸入圖片說明

您可以使用C#關鍵字“ is”來查看對象是否與給定類型兼容。 http://msdn.microsoft.com/zh-CN/library/vstudio/scekt9xw.aspx

假設您的意思是兩個List<T>都具有匹配的T ,則可以使用:

private static Boolean MatchingBaseType(IEnumerable a, IEnumerable b)
{
    return GetIListBaseType(a) == GetIListBaseType(b);
}

private static Type GetIListBaseType(IEnumerable a)
{
    foreach (Type interfaceType in a.GetType().GetInterfaces())
    {
        if (interfaceType.IsGenericType &&
            (interfaceType.GetGenericTypeDefinition() == typeof(IList<>) ||
             interfaceType.GetGenericTypeDefinition() == typeof(IEnumerable<>) ||
             interfaceType.GetGenericTypeDefinition() == typeof(ICollection<>))
        )
        {
            return interfaceType.GetGenericArguments()[0];
        }
    }
    return default(Type);
}

您說count無關緊要(盡管您正在檢查.Count()為什么?),但是如果兩個列表中具有相同的類型,則應該返回此值。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM