score:4
Is this LINQ to SQL? I suspect the problem is that you're ordering before the grouping. You could try this:
var q = dc.tblHelpCentreQuestions
.Where(question => question.userID == UserID)
.Where(question => question.awaitingUserResponse
|| question.awaitingSupportResponse)
.GroupJoin(dc.tblHelpCentreReplies,
question => question.ID,
replies => replies.ticketID,
(question, replies) => new { Question = question,
RepliesCount = replies.Count() })
.OrderBy(s => s.Question.awaitingUserResponse)
.ThenBy(s => s.Question.dateSubmitted);
EDIT: Okay, if this isn't working then perhaps it's a limitation of SQL grouping... although that seems pretty odd.
You could always force the ordering to be performed at the client side instead though:
var q = dc.tblHelpCentreQuestions
.Where(question => question.userID == UserID)
.Where(question => question.awaitingUserResponse
|| question.awaitingSupportResponse)
.GroupJoin(dc.tblHelpCentreReplies,
question => question.ID,
replies => replies.ticketID,
(question, replies) => new { Question = question,
RepliesCount = replies.Count() })
// Force the rest of the query to execute in .NET code (Enumerable.XXX)
.AsEnumerable()
.OrderBy(s => s.Question.awaitingUserResponse)
.ThenBy(s => s.Question.dateSubmitted);
score:2
It’s hard to test your code because I don’t have all your declarations, but I suspect that the reason you are getting seemingly random behaviour is because the GroupJoin
makes no guarantees of keeping the order intact.
Therefore, you will have to do the ordering after the grouping.
For example:
var q = dc.tblHelpCentreQuestions
.Where(question => question.userID == UserID)
.Where(question => question.awaitingUserResponse == true || question.awaitingSupportResponse == true)
.GroupJoin(
dc.tblHelpCentreReplies,
question => question.ID,
replies => replies.ticketID,
(question, replies) => new { Question = question, RepliesCount = replies.Count() }
)
.OrderBy(s => s.Question.awaitingUserResponse)
.ThenBy(s => s.Question.dateSubmitted);
score:2
try adding ToArray() after your LINQ query. I do know that LINQ follows lazy evaluation rule, and ToArray() forces evaluation to be eager.
score:3
Have you tried moving the OrderBy to the very last operation, just before the ;? Like SQL, I wouldn't expect to receive an ordered set if I performed ANY operations on it after I did the ordering... I'd be curious to see what the actual SQL generated is when presented in that order.
Edit for code exploring using ToList() to force execution, then do the ordering clientside:
var q = dc.tblHelpCentreQuestions
.Where(question => question.userID == UserID)
.Where(question => question.awaitingUserResponse == true || question.awaitingSupportResponse == true)
.GroupJoin(
dc.tblHelpCentreReplies,
question => question.ID,
replies => replies.ticketID,
(question, replies) => new { Question = question, RepliesCount = replies.Count() }
)
.ToList()
.OrderBy(s => s.Question.awaitingUserResponse)
.ThenBy(s => s.Question.dateSubmitted);
score:3
The GroupJoin is most likely overriding the order. It's likely implemented as a subquery. If you run either LINQ to SQL Profiler or SQL profiler to see the underlying query, it would be able to shed light on this. Have you tried doing OrderBy as the last operation in the method chain?
score:3
If you want to order your grouped questions, you need to do that after you create the GroupJoin:
var q =
dc.tblHelpCentreQuestions.Where(question => question.userID == UserID).Where(question => question.awaitingUserResponse == true || question.awaitingSupportResponse == true).
GroupJoin(
dc.tblHelpCentreReplies,
question => question.ID,
replies => replies.ticketID,
(question, replies) => new { Question = question, RepliesCount = replies.Count() }
).
AsEnumerable().
OrderBy(s => s.Question.awaitingUserResponse).
ThenBy(s => s.Question.dateSubmitted);
GroupJoin
will effectively remove your ordering, since it's taking your ordered collection and grouping it by question. GroupJoin
does not preserve the initial ordering of the keys.
Edit: You can eliminate this issue by forcing the ordering to occur in LINQ to Objects, by converting to an enumerable after the GroupJoin.
Source: stackoverflow.com
Related Query
- Linq order by isn't ordering anything
- LINQ Source Code Available
- creating Linq to sqlite dbml from DbLinq source code
- Linq order by not ordering
- Why the extension method of where for LINQ in this code would print out a single number while it shouldn't print anything at all?
- source code for LINQ 101 samples
- Will Linq query code in C# UWP project work slower or faster depending on order of its' parts?
- Linq query for ordering by ascending order except for one row
- c# Linq or code to extract groups from a single list of source data
- Suggestions on how to optimize code for ordering by string in linq
- Preserving order with LINQ
- Convert string[] to int[] in one line of code using LINQ
- Multiple Order By with LINQ
- Code equivalent to the 'let' keyword in chained LINQ extension method calls
- LINQ order by null column where order is ascending and nulls should be last
- Linq to Objects: does GroupBy preserve order of elements?
- Linq order by boolean
- Does the order of LINQ functions matter?
- Linq code to select one item
- C# - code to order by a property using the property name as a string
- Linq Order by a specific number first then show all rest in order
- ascending/descending in LINQ - can one change the order via parameter?
- Linq order by, group by and order by each group?
- How are people unit testing code that uses Linq to SQL
- LINQ orderby on date field in descending order
- Conditional "orderby" sort order in LINQ
- LINQ group by then order groups of result
- How do I get LINQ to order according to culture?
- Should the order of LINQ query clauses affect Entity Framework performance?
- linq multiple order DESCENDING
More Query from same tag
- Is there an optimized way of getting business weeks between two dates?
- Linq equivalent of Except
- using LINQ to get the top N percent of a list
- fill linq null data c# from previous line
- LINQ query syntax
- lambda expressions in immediate window for VS2015
- ASP.NET LINQ SQL get specific fields
- What construction can I use instead of Contains?
- How can I select the most recent and distinct records and then count the different result types using LINQ?
- How to get lambda expression parameters
- Single enumeration with multiple filters
- Some help on a Union LINQ expression please
- the ForEach Extension Method on the List - how to get the object?
- Why does AsQueryable throw a StackOverflowException?
- Should I always call .ToArray on LINQ query results returned in a function?
- LINQ nested select
- LINQ single record forms or defaults if no record
- LINQ Query for Average of column with a join, grouping by the key of the joined table
- LINQ part of string is contained in list members
- Insert data from array to datatable
- Join datatables with LINQ, select multiple columns and sums with group by multiple columns
- Find parent class from property
- Most efficient way to determine if there are any differences between specific properties of 2 lists of items?
- Set Properties that not null using linq and reflection
- query is returning list of anonymous types, want list of values instead
- Complexity of swapping Dictionary type arguments in C#
- How to get a Lookup from nested dictionaries
- LINQ - can't create a request
- Odd Linq behavior with IList / IEnumerable
- C# Linq XML Query where multiple elements of same name from a parent node based on a child node value