0

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.

2 Answers 2

3

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; 
Sign up to request clarification or add additional context in comments.

9 Comments

I think m.Procudures is a collection (because of 0 or more)
@MarcinJuraszek I thought that initially (because it's plural), but OP's current query uses m.Procedures.Processes.processID == id. If the current code compiles at all, it probably isn't a collection.
Didn't notice that. But that's why correct naming is a key in writing good code...
@MarcinJuraszek How should it have been named? The Procedures table houses a collection of Procedures. Should I have named it Procedure?
If it's just a single Procedure from Procedures table then yes, the property should be called Procedure.
|
1
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) 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.