Skip to main content

LINQ Sort Operators - IEnumerable and IOrderedEnumerable

Consider following example
var query = Process.GetProcessess().
.OrderBy(p => p.WorkingSet64)
.ThenOrderBy(p => p.ThreadCount)

In this case query is of type IOrderedEnumerable

Hence you cannot user a Where Operation like this

query = query.Where(p => p.ProcessName.Length < 10

You will get a compile error that you cannot IEnumerable to IOrderedEnumerable

However if you add Where operator on first query, it will return IEnumaerable

e.g

var query = Process.GetProcessess().
.OrderBy(p => p.WorkingSet64)
.ThenOrderBy(p => p.ThreadCount)
.Where(p => p.ProcessName.Length < 10)

Comments