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;
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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With