Click here to Skip to main content
15,895,011 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Is there a way to get some selected data first & rest data in ascending order through SQL Query.
C#
Suppose I have country data in unmannered way (not in sequence) in table tbl. Now I want to show Country  'United States','Canada' & 'India' first & rest of the country shown in ascending order.


C#
Country

India
China
Brazil
Azerbaijan
Bahamas
United State
Denmark
Canada

Now I want this data as :
C#
Country

United State        // US,Canada,India should be First 3.
Canada
India
Azerbaijan         // After rest country comes in ascending Order.
Bahamas
Brazil
China
Denmark


Any suggestion really appreciate.
Posted

select * from tblName
Order by
case When country = 'United State' then 1 when country = 'Canada' then 2 when country = 'India' then 3 else 4 end , country

question : How to write this query in Linq query in c#
 
Share this answer
 
Comments
Richard Deeming 29-Aug-23 4:49am    
Your question is not a "solution" to someone else's question.

If you want to ask a question, then ASK A QUESTION[^].
SQL
select * from tblName
Order by
case When country = 'United State' then 1 when country = 'Canada' then 2 when country = 'India' then 3 else 4 end , country  

Happy Coding!
:)
 
Share this answer
 
v2
Comments
Mas11 28-May-13 8:31am    
Really a good answer! Thanks a lot Aarti : )
Aarti Meswania 28-May-13 8:33am    
Welcome!
Glad to help you! :)
Basmeh Awad 28-May-13 8:33am    
+5
Aarti Meswania 28-May-13 8:33am    
Thank you! :)
Basmeh Awad 29-May-13 6:00am    
hi aarti..whats up?? please check this out
http://www.codeproject.com/Questions/599752/CreateplusorplusReplaceplustable

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900