Comparing Arrays using LINQ in C#

前端 未结 6 957
闹比i
闹比i 2020-12-15 04:46

I\'ve two arrays like

string[] a = { \"a\", \"b\", \"c\" };
string[] b = { \"a\", \"b\", \"c\" };

I need to compare the two arrays using LI

6条回答
  •  余生分开走
    2020-12-15 05:23

    Not sure about the performance, but this seems to work.

    string[] a = { "a", "b", "c" };
    string[] b = { "a", "b", "c" };
    
    bool result = a.SequenceEqual(b);
    Assert.AreEqual(true, result);
    

    However, it is not order independent so it does not fulfill the OP's requirement.

    string[] a = { "a", "b", "c" };
    string[] b = { "a", "c", "b" };
    
    bool result = a.SequenceEqual(b);
    Assert.AreEqual(false, result);
    

提交回复
热议问题