Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Linq to SQL - How to sort results from query

I have a LINQ query I am retrieving, although in its context how would I sort the output, I have tried sortby etc. but to no avail

    DataClasses1DataContext db = new DataClasses1DataContext();
    var returnall = from p in db.Orders
                    select p.ShipName;
like image 599
James Avatar asked May 15 '11 15:05

James


People also ask

How do you sort the results of a query?

Use the ORDER BY clause to order the rows selected by a query. Sorting by position is useful in the following cases: To order by a lengthy select list expression, you can specify its position in the ORDER BY clause rather than duplicate the entire expression.

How does a LINQ query transform to a SQL query?

LINQ to SQL translates the queries you write into equivalent SQL queries and sends them to the server for processing. More specifically, your application uses the LINQ to SQL API to request query execution. The LINQ to SQL provider then transforms the query into SQL text and delegates execution to the ADO provider.

How do I sort in LINQ?

LINQ includes five sorting operators: OrderBy, OrderByDescending, ThenBy, ThenByDescending and Reverse. LINQ query syntax does not support OrderByDescending, ThenBy, ThenByDescending and Reverse. It only supports 'Order By' clause with 'ascending' and 'descending' sorting direction.

How do I sort a list in ascending order LINQ?

In LINQ, the OrderBy operator is used to sort the list/ collection values in ascending order. In LINQ, if we use order by the operator by default, it will sort the list of values in ascending order. We don't need to add any ascending condition in the query statement.


1 Answers

var returnall = from p in db.Orders
                orderby p.ShipName
                select p.ShipName;

A handy reference for various LINQ functions can be found on the MSDN samples page.

like image 141
SirViver Avatar answered Sep 29 '22 00:09

SirViver