i'm got a simple IEnumerable<foo> foos;
This collection are some results from a db call.
I wish to make sure that each item in the list is ordered correctly. The Db either returns the results by
Can this be done with a quick linq statement .. to check for this?
eg. Assert.IsTrue(.. insert linq statement here .. )
currently, i've got a for loop and remember the previous entry and check if the value of that, if there was a value. This feels ... cumbersome.
Any ideas?
You could compare your collection to an ordered version:
CollectionAssert.AreEqual(foos.ToList(), foos.OrderBy(f => f.Id));
Or:
CollectionAssert.AreEqual(foos.ToList(),
foos.OrderByDescending(f => f.DateCreated));
There is an extension method called SequenceEqual which will do that. However, there is also an assert method called CollectionAssert.AreEqual that compares two collections to ensure they have the same items in the same order.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With