James Wilson
James Wilson

Reputation: 5150

Trying to order LINQ results

To one of my tables I added a sortOrder. I now need to update this query to respect the sortOrder authority!

Here is my current query:

var HTMLdocs = db.ProcedureDocs.Where(m => m.Procedures.Processes.processID == id && m.Document.html != null && m.Document.approvedBy != null).Select(m => m.Document);

The table that has sortOrder is Procedures.

How can I get the above query to return results that are ordered by sortOrder.

Here is the table structure.

Processes can have 0 or many Procedures.
Document can have 0 or many ProcedureDocs.

ProcedureDocs has a foreign key to Procedures on procedureid.

I somehow need to grab a collection of Document that somehow respects the order found in the Procedures table.

Let me know if you need any other information.

Upvotes: 0

Views: 66

Answers (2)

Dan Hunex
Dan Hunex

Reputation: 5318

var HTMLdocs = db.ProcedureDocs
    .Where(m => m.Procedures.Processes.processID == id 
       && m.Document.html != null && m.Document.approvedBy != null)

    .OrderBy(x=>x.Procedures.sortOrder)
    .Select(m => m.Document)

Upvotes: 1

p.s.w.g
p.s.w.g

Reputation: 149058

Try something like this:

var HTMLdocs = db.ProcedureDocs
    .Where(m => m.Procedures.Processes.processID == id && 
                m.Document.html != null && 
                m.Document.approvedBy != null)
    .OrderBy(m => m.Procedures.sortOrder)
    .Select(m => m.Document);

Or in query syntax:

var HTMLdocs = 
    from m in db.ProcedureDocs
    where m.Procedures.Processes.processID == id && 
          m.Document.html != null && 
          m.Document.approvedBy != null
    orderby m.Procedures.sortOrder
    select m.Document;

Upvotes: 3

Related Questions