English 中文(简体)
将2个清单与不同的参考(C#)进行比较
原标题:Comparing 2 lists with different references (C#)
  • 时间:2011-04-15 15:17:38
  •  标签:
  • c#
  • linq
  • list

基本上,我有一个包含若干领域的内容。 我将编制两个清单,列出这种结构的事例,我需要能够检查这两个清单是否相同(即,所有领域的结构相同,具有相同的价值)。

我正在研究公平经营人,但这取决于名单上有相同<>>>>><>>>>的物品,他们都从不同来源收回。

我只能命令实地填写清单,按每个项目填写,然后在清单中逐个实地/财产,看看清单是否与另一个清单相符,但似乎只是克服了。 是否有较容易的办法?

最佳回答

<代码>SequenceEqual的操作者使用比较器而不是参照顺序进行对比。

From MSDN: "Determines whether two sequences are equal by comparing the elements by using the default equality comparer for their type."

Use the SequenceEqual and implement a IEqualityComparer and you should be fine.

var areSame = list1.SequenceEqual(list2, new MyStructComparer());

class MyStructComparer : IEqualityComparer<MyStruct> {
    public bool Equals(MyStruct x, MyStruct y) {
        if (Object.ReferenceEquals(x, y)) 
            return true;
        if (Object.ReferenceEquals(x, null) || Object.ReferenceEquals(y, null))
            return false;
        return x.A == y.A && x.B == y.B;
    }

    public int GetHashCode(MyStruct myStruct)
    {        
        if (Object.ReferenceEquals(myStruct, null)) 
            return 0;

        int hashMyStructA = myStruct.A == null ? 0 : myStruct.A.GetHashCode();
        int hashMyStructB = myStruct.B == null ? 0 : myStruct.B.GetHashCode();

        return hashMyStructA ^ hashMyStructB;
    }   
}
问题回答

You can override Equals() and compare all the properties in there and return true if they are all equal.

    public struct MyStruct
    {

        public int A;
        public int B;

        public override bool Equals(object obj)
        {
            MyStruct other = (MyStruct) obj;
            return A == other.A && B == other.B;
        }
    }

For equality of the lists, I would write an extension method:

    public static class MyStructExtension
    {
        public static bool ListsEqual(this IList<MyStruct> a, IList<MyStruct> b)
        {
            if(a==b)
                return true;
            if (a == null || b == null)
                return false;
            if (a.Count != b.Count)
                return false;
            for (int i = 0; i < a.Count; i++)
            {
                if (!a[i].Equals(b[i]))
                    return false;
            }

            return true;
        }
    }




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签