|
-
Dec 20th, 2010, 09:35 AM
#1
how to sort this ?
hi all i have a little problem i need to order data from my database in this manner:
let's say i have list of orders
Code:
Company ORDER | ORDER TIME
-----------------------------------------
Company A | Order | 10:00
Company B | Order | 10:01
Company A | Order | 10:02
Company B | Order | 10:10
Company C | Order | 10:03
-----------------------------------------
Now i want to sort the orders by Order Time and Company Name since Company A will be the first on the list (since it has the earlier order) i now want that all it's other orders of that company will be displayed.
I hope you get what i mean, the final order should be something like that:
Code:
Company A - 10:00;
Company A - 10:02;
Company B - 10:01;
Company B - 10:10;
Company C - 10:03;
before i get into code this i wanted to ask if there is quick LINQ solution to this ?
* Rate It  If you Like it
__________________________________________________________________________________________
" Programming is like sex: one mistake and you’re providing support for a lifetime."
Get last SQL insert ID 
-
Dec 20th, 2010, 09:55 AM
#2
Re: how to sort this ?
You can query it from the database:
Code:
SELECT * FROM Orders ORDER BY Company;
-
Dec 20th, 2010, 09:58 AM
#3
Re: how to sort this ?
But if Company A does not have the earliest time then the result will be different. Assume Company A has time 10:04 and 10:06 . Then Order By company will not give the desired result.
Company A will be the first on the list (since it has the earlier order
-
Dec 20th, 2010, 09:59 AM
#4
Re: how to sort this ?
I think you need to use Order by and Group by .
-
Dec 20th, 2010, 10:01 AM
#5
Re: how to sort this ?
mmm.. i wish it was so simple,
I might didn't explained well, i don't want the list to be ordered by company name, i want it to be ordered first by orders time and inside that order i want it to "reorder it" so all the orders of the company which has the first will be displayed and so on...
* Rate It  If you Like it
__________________________________________________________________________________________
" Programming is like sex: one mistake and you’re providing support for a lifetime."
Get last SQL insert ID 
-
Dec 20th, 2010, 10:17 AM
#6
Re: how to sort this ?
You mean a secondary sort like this:
Code:
SELECT * FROM Orders ORDER BY Order, Company;
-
Dec 20th, 2010, 10:18 AM
#7
Re: how to sort this ?
I think you can do it in a SQL statement but you'd need to group by all the columns as you have no aggregate function.
Code:
Select Company, Order, OrderTime From Table
Group By Company, Order, OrderTime
Order By OrderTime
Not sure if you can do the same with LINQ though.
Alex
.NET developer
"No. Not even in the face of Armageddon. Never compromise." (Walter Kovacs/Rorschach)
Things to consider before posting.
Don't forget to rate the posts if they helped and mark thread as resolved when they are.
.Net Regex Syntax (Scripting) | .Net Regex Language Element | .Net Regex Class | DateTime format | Framework 4.0: what's new
My fresh new blog : writingthecode, even if I don't post much.
System: Intel i7 920, Kingston SSDNow V100 64gig, HDD WD Caviar Black 1TB, External WD "My Book" 500GB, XFX Radeon 4890 XT 1GB, 12 GBs Tri-Channel RAM, 1x27" and 1x23" LCDs, Windows 10 x64, ]VS2015, Framework 3.5 and 4.0 
-
Dec 20th, 2010, 10:21 AM
#8
Re: how to sort this ?
no cicatrix, thats not what i meant, stlaural i hoped i won't have to sort the list twice (first by group by and then the actual list), I'll wait a bit to see if there is any better way of doing this if not i'll just code the thing.
* Rate It  If you Like it
__________________________________________________________________________________________
" Programming is like sex: one mistake and you’re providing support for a lifetime."
Get last SQL insert ID 
-
Dec 20th, 2010, 10:42 AM
#9
Re: how to sort this ?
If you build your list from your database with an SQL statement like the one I posted, Your list would already be ordered the way you want it.
If your list is already ordered by "Order Time" I'd simply try to do a GroupBy including all the columns with linq. I think its worth the try to avoid coding it.
Alex
.NET developer
"No. Not even in the face of Armageddon. Never compromise." (Walter Kovacs/Rorschach)
Things to consider before posting.
Don't forget to rate the posts if they helped and mark thread as resolved when they are.
.Net Regex Syntax (Scripting) | .Net Regex Language Element | .Net Regex Class | DateTime format | Framework 4.0: what's new
My fresh new blog : writingthecode, even if I don't post much.
System: Intel i7 920, Kingston SSDNow V100 64gig, HDD WD Caviar Black 1TB, External WD "My Book" 500GB, XFX Radeon 4890 XT 1GB, 12 GBs Tri-Channel RAM, 1x27" and 1x23" LCDs, Windows 10 x64, ]VS2015, Framework 3.5 and 4.0 
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|