views:

19

answers:

1

I am trying to do something like this:

 ViewData.Model = _db.Questions  
                     .Include("QType")  
                     .Include("QTags")  
                     .Include("SubQuestions.Options")  
                     .Where(q => q.Active == true)  
                     .Orderby(q => Questions.Order)  
                     .Orderby(sq => SubQuestions.Order)  
                     .ToList(); 

But ofcourse it is not working as I want to. The Ordering works on Question.Order, but I would also the Questions.SubQuestions List to be ordered according to SubQuestions.Order Any blatant mistakes anyone can help me resolve?

Thanks

A: 

Is your code accurate? It doesn't look like you're using either q or sq in your order by clauses. I'm pretty sure your problem though is that your second order by should be replaced with the ThenBy operator.

Kirk Woll
Hi Kirk, I also tried this: .Thenby(q => q.SubQuestions.Orderby(sq => sq.SQOrder) ) , but this doesn't work either. Could you give any tips / pointers?
Priyeshj
@Priyeshj, you are returning a sequence of questions. Each question can have *many* sub questions, correct? What does it mean to sort on a column on the sub question table when returning results on the question? In other words, if each question can have multiple sub-questions, then it doesn't make sense to order by those subquestions when you are returning the top-level questions. Perhaps you want the MAX Order value for the sub-question? Can you perhaps show some sample data that describes the results you expect?
Kirk Woll
@Kirk: I guess you might be correct. I ended up creating a separate View Model for the relationships, and sorting then dynamically in the views instead of model/controller. So no more .Thenby(...). Bad implementation, but works. Thanks for the clues.
Priyeshj