I am querying a table of Order elements with LINQ (C#). Each Order has the following fields:
- ID
- OpenDate
- PriorityID
- StatusID
- Description
The StatusID field maps to a Status table. The Status table is structured as:
- ID
- Name
I need to get all of the Order objects sorted by their Priority and Status.
I can successfully get the Order objects sorted by Priority. I’m doing this via the
following:
List<Order> orders = new List<Order>();
using (DBDataContext context = new DBDataContext())
{
orders = (from o in context.Orders
orderby (o.PriorityID.HasValue ? o.PriorityID : Int32.MaxValue) ascending
select o).ToList();
}
But my problem is factoring in the Status.
Once the order objects have been sorted by priority, I need to sort the Order objects
in the following order of Status: Cancelled, Open, In-Route, and Delivered. Significantly,
The IDs of these Status values are firmly set in a random, non-helpful order. I cannot alter them. As you can tell, I can’t sort the status by alphbetical name either. In addition, I can’t add any fields to my database. Can anyone tell me how I can solve this problem in LINQ?
Thank you!
I think you have solution to this by implement IComparer and use Linq to order it. Since your Status is not in numeric nor alphabet order.
then
Hope this helps.