0

I have the following

List<WorkItemHeader>
WorkItemWorkItemHeader(string company, string division, string customerNumber, string imgCode, int sequence, string orderNumber, int lineNumber, DateTime importDate, int processType, int operatorID, int typeset, string product)

I would like to order my list by multiple properties (by Division, ImportDate, OrderNumber, LineNumber)

Division contains (ABC, XYZ, DEF, HIJ)

My requirement (and question) is, Division needs to be sorted by Division containing: (XYZ, ABC) first then by ImportDate, OrderNumber, LineNumber.

MySql has FIELD()

ORDER BY FIELD(position,'forward', 'midfielder', 'defender', 'goalkeeper')

T-SQL has CASE WHEN

ORDER BY CASE Position WHEN "forward" THEN 1
                   WHEN "defender" THEN 2
                   WHEN "midfielder" THEN 3
                   WHEN "goalkeeper" THEN 4 END

How can I simulate the sorting using Linq?

Mike
  • 71
  • 7
  • [See here](https://stackoverflow.com/questions/298725/multiple-order-by-in-linq) – kay Apr 11 '18 at 15:50
  • That would work if I was simply ordering suchas: WorkItemHeaders = (List)WorkItemHeaders.OrderBy(x => x.Division).ThenBy(x => x.ImportDate).ThenBy(x => x.OrderNumber).ThenBy(x => x.LineNumber); I need Division ordered in a certain order using string values first. – Mike Apr 11 '18 at 15:52

1 Answers1

0

It sounds like you want to just get Division XYZ and ABC first, order those by the fields you specified, then order the rest, and then combine those results? You did not specify how you wanted to order the remaining divisions or the other fields, so I just did the default ascending order.

var firstOrder = header.Where(d => d.Division == "XYZ" || d.Division == "ABC").OrderByDescending(c => c.Division).ThenBy(d => d.ImportDate).ThenBy(d => d.OrderNumber).ThenBy(d => d.LineNumber).ToList();
var secondOrder = header.Where(d => d.Division != "XYZ" && d.Division != "ABC").OrderBy(c => c.Division).ThenBy(d => d.ImportDate).ThenBy(d => d.OrderNumber).ThenBy(d => d.LineNumber).ToList();
firstOrder.AddRange(secondOrder);
Justin Loveless
  • 524
  • 1
  • 10
  • 25