HiveBrain v1.2.0
Get Started
← Back to all entries
patterncsharpMinor

Checking whether relations are equal to foreign fields using LINQ Any()

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
linqrelationsareequalfieldscheckingforeignanyusingwhether

Problem

Can you find a more understandable way to write the following one-liner?

Enumerable.Range(0, relation.Fields.Count).
           Any(i => relation.Fields[i] != relation.ForeignFields[i])


but still preserving its brevity?

Simply put, I wanna cycle on relation.Fields collection to see if their string elements are one-by-one equal to relation.ForeignFields ones.

The reason I had to use Enumerable.Range is that I need an index, in order to cycle the two collections.

Note: they have for sure the same count.

I wrote an extension method based on @t3chb0t's answer

Solution

Can you not simply do SequenceEqual?

var list = new List { "test1", "test2", "test3" };
var list2 = new List { "test1", "test2", "test3" };

var areListsEqual = list.SequenceEqual(list2);

Code Snippets

var list = new List<string> { "test1", "test2", "test3" };
var list2 = new List<string> { "test1", "test2", "test3" };

var areListsEqual = list.SequenceEqual(list2);

Context

StackExchange Code Review Q#135604, answer score: 9

Revisions (0)

No revisions yet.