Suppose I have a class with an integer Day
property and an IEnumerable
of objects where the days are 2, 3, 4, 1, 3, 3 and 5 (in that order
Is there a way to guarantee that the order of the subsequence where
Assuming you have class like this:
class A
{
public string Name { get; set; }
public int Day { get; set; }
// other properties
}
and the sequence:
{ "A", 2 },
{ "B", 3 },
{ "C", 4 },
{ "D", 1 },
{ "E", 3 },
{ "F", 3 },
{ "G", 5 },
If you mean, will this:
sequence.Where(item => item.Day == 3)
produce the sequence, where items will be ordered like this: B, E, F
, then the answer is "no, you nave no guarantee".
If your sequence is a List
, than ordering will be preserved (indeed, it will be preserved with LINQ to Objects, not only with lists).
If you sequence is IQueryable
, then the ordering may depend from the LINQ provider implementation, underlying data source and current expression tree, already contained in
IQueryable
. So, in this case you should force ordering with
OrderBy
/OrderByDescending
.