" /> Multiple "commande par" dans LINQ | 2022 codeprofesseur

Multiple "commande par" dans LINQ

Mots clés : linqsql-order-bylinq

meilleur 5 Réponses Multiple "commande par" dans LINQ

vote vote

94

var movies = _db.Movies.OrderBy(c => c.Category).ThenBy(n => n.Name) 
vote vote

87

var movies = from row in _db.Movies               orderby row.Category, row.Name              select row; 
var movies = from row in _db.Movies               orderby row.Category descending, row.Name              select row; 
vote vote

78

var movies = _db.Movies.OrderBy( m => new { m.CategoryID, m.Name }) 
var movies = _db.Movies.OrderBy( m => (m.CategoryID.ToString() + m.Name)) 
vote vote

64

_db.Log = Console.Out 
var movies = from row in _db.Movies               orderby row.CategoryID, row.Name              select row; 
var movies = _db.Movies.OrderBy(m => m.CategoryID).ThenBy(m => m.Name); 
SELECT [t0].ID, [t0].[Name], [t0].CategoryID FROM [dbo].[Movies] as [t0] ORDER BY [t0].CategoryID, [t0].[Name] 
var movies = from row in _db.Movies               orderby row.CategoryID              orderby row.Name              select row; 
var movies = _db.Movies.OrderBy(m => m.CategoryID).OrderBy(m => m.Name); 
SELECT [t0].ID, [t0].[Name], [t0].CategoryID FROM [dbo].[Movies] as [t0] ORDER BY [t0].[Name], [t0].CategoryID 
vote vote

56

// We do not have to care if the queryable is already sorted or not.  // The order of the Smart* calls defines the order priority queryable.SmartOrderBy(i => i.Property1).SmartOrderByDescending(i => i.Property2); 
public static class IQueryableExtension {     public static bool IsOrdered<T>(this IQueryable<T> queryable) {         if(queryable == null) {             throw new ArgumentNullException("queryable");         }          return queryable.Expression.Type == typeof(IOrderedQueryable<T>);     }      public static IQueryable<T> SmartOrderBy<T, TKey>(this IQueryable<T> queryable, Expression<Func<T, TKey>> keySelector) {         if(queryable.IsOrdered()) {             var orderedQuery = queryable as IOrderedQueryable<T>;             return orderedQuery.ThenBy(keySelector);         } else {             return queryable.OrderBy(keySelector);         }     }      public static IQueryable<T> SmartOrderByDescending<T, TKey>(this IQueryable<T> queryable, Expression<Func<T, TKey>> keySelector) {         if(queryable.IsOrdered()) {             var orderedQuery = queryable as IOrderedQueryable<T>;             return orderedQuery.ThenByDescending(keySelector);         } else {             return queryable.OrderByDescending(keySelector);         }     } } 

Questions similaires