Wednesday, April 11, 2012

How to Order by Multiple in Linq

When Ordering custom collection in Linq  using multiple fields you can use OrderBy and ThenBy methods.


private BOMLineDTO[] SortBOMLines(BOMLineDTO[] bomLines)
BOMLineDTO[] sortedBOMLines = null;

if (null != bomLines)
sortedBOMLines = bomLines.OrderBy(record => record.ItemDescription, new BOMLineComparer<StringField>()).
ThenBy(record => record.BOMCategoryType, new BOMLineComparer<BasicDataField>()).
ThenBy(record => record.GMTColor, new BOMLineComparer<BasicDataField>()).
ThenBy(record => record.GMTSize, new BOMLineComparer<BasicDataField>()).ToArray();
return sortedBOMLines;
Here BOMLineComparer<T> is My own class which is derived form IComparer<T> interface

Related Posts: