Compare the order of two lists using Linq
July 15, 2009
Just had the need to compare that a list was in the correct order for a test I’d written. Handily there is a Linq extension method that makes the job easy, as the following snippet shows.
private static bool
OrdersMatch(IEnumerable newsItems, IEnumerable newsIds)
{
return newsItems.Select(n => n.Id).SequenceEqual(newsIds);
}
You can use an extension method as I have, if you need to match on a specific part of the objects you are comparing.