Linq question

  • Thread starter Thread starter Michael Knudsen
  • Start date Start date
M

Michael Knudsen

Hi all.

I have a problem with a linq query.
I'm working on a timesheet project.
Given the following linq query:

var query = from L in listTimesheetRecord
select new
{
Copy = "",
Posted = L.Posted.ToString(),
ClientName = L.ClientName,
MatterID = L.MatterId,
MatterName = L.MatterName,
Language = L.LanguageId,
TransText = L.TransText,
HourMon = L.HourMonday,
HourTue = L.HourTuesday,
HourWed = L.HourWednesday,
HourThu = L.HourThursday,
HourFri = L.HourFriday,
HourSat = L.HourSaturday,
HourSun = L.HourSunday,
recid = L.RecId
};
query = query.OrderBy(o => o.HourTue).ThenBy(p => p.ClientName).ThenBy(q =>
q.MatterID);

I just descovered that the query doesn't return records in the expected
order.
What I wish for is (in this query): give me all records which have a value
greater than zero and then order the remaining records by ClientName and
MatterID. The current query returnes records sortet by hourTue and then by
ClientName/MatterID
e.g.
0 - company A - xxxx
20 - company A - xxxx
1 - company B - xxxx
2 - company B - xxxx

when the desired sortorder is:
0 - company A - xxxx
1 - company B - xxxx
2 - company B - xxxx
20 - company A - xxxx

I know a dirty solution could be just to remove the Thenby-clause but I may
have a lot of records registered on the same Clientname and matterID. Could
anyone help me out here?!

Thanks.
/Michael
 
Michael said:
Hi all.

I have a problem with a linq query.
I'm working on a timesheet project.
Given the following linq query:

var query = from L in listTimesheetRecord
select new
{
Copy = "",
Posted = L.Posted.ToString(),
ClientName = L.ClientName,
MatterID = L.MatterId,
MatterName = L.MatterName,
Language = L.LanguageId,
TransText = L.TransText,
HourMon = L.HourMonday,
HourTue = L.HourTuesday,
HourWed = L.HourWednesday,
HourThu = L.HourThursday,
HourFri = L.HourFriday,
HourSat = L.HourSaturday,
HourSun = L.HourSunday,
recid = L.RecId
};
query = query.OrderBy(o => o.HourTue).ThenBy(p => p.ClientName).ThenBy(q
=> q.MatterID);

I just descovered that the query doesn't return records in the expected
order.
What I wish for is (in this query): give me all records which have a
value greater than zero and then order the remaining records by
ClientName and MatterID. The current query returnes records sortet by
hourTue and then by ClientName/MatterID
e.g.
0 - company A - xxxx
20 - company A - xxxx
1 - company B - xxxx
2 - company B - xxxx

when the desired sortorder is:
0 - company A - xxxx
1 - company B - xxxx
2 - company B - xxxx
20 - company A - xxxx

I know a dirty solution could be just to remove the Thenby-clause but I
may have a lot of records registered on the same Clientname and
matterID. Could anyone help me out here?!

Thanks.
/Michael

What data type is the HourTuesday data in the L object? That kind of
ordering looks rather random to me.

I would understand if the order was 0, 1, 2, 20, 3, 4, 40, 5, because
that would look like an alphabetical ordering, but 0, 20, 1, 2, that's
unfamiliar.

Or perhaps I'm misunderstanding the purpose of the ThenBy ordering?
What I'm assuming is that your ordering is akin to the following SQL:

ORDER BY HourTue, ClientName, MatterID

On the other hand, if ThenBy and OrderBy is stable sorting, the ordering
could very well be reversed if I'm misunderstanding how OrderBy and
ThenBy works, in that it would first sort the entire list by HourTue,
and then the entire list again by ClientName, keeping the order of
HourTue stable within each client.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

How to order by a variable column name using linq?? 2
Question about LinQ (LinQ to Sql) 4
Problem ... 2
Linq. Take and OrderBy 7
Linq Order By 2
Linq. OrderBy and Where. 7
LINQ "IN" Question 1
LINQ Help 4

Back
Top