MVC, Linq - 排序问题



我收到此错误:

表达式必须具有顺序可比的类型。参数名称:键 |at System.Data.Common.CommandTrees.ExpressionBuilder.Internal.ArgumentValidation.ValidateSortClause(DbExpression key( at System.Data.Common.CommandTrees.ExpressionBuilder.DbExpressionBuilder.ToSortClause(DbExpression key( at System.Data.Objects.ELinq.ExpressionConverter.MethodCallTranslator.OrderByTranslatorBase.TranslateOneLambda(ExpressionConverter parent, DbExpressionBinding sourceBinding, DbExpression lambda( atSystem.Data.Objects.ELinq.ExpressionConverter.MethodCallTranslator.OneLambdaTranslator.translate(ExpressionConverter parent, MethodCallExpression call, DbExpression& source, DbExpressionBinding& sourceBinding, DbExpression& lambda( at System.Data.Objects.ELinq.ExpressionConverter.MethodCallTranslator.OneLambdaTranslator.translate(ExpressionConverter parent, MethodCallExpression call( call( atSystem.Data.Objects.ELinq.ExpressionConverter.MethodCallTranslator.SequenceMethodTranslator.Translate(ExpressionConverter parent, MethodCallExpression call, SequenceMethod sequenceMethod( at System.Data.Objects.ELinq.ExpressionConverter.MethodCallTranslator.TypedTranslate(ExpressionConverter parent, MethodCallExpression linq( at System.Data.Objects.ELinq.ExpressionConverter.TypedTranslator 1.Translate(ExpressionConverter parent, Expression linq) at System.Data.Objects.ELinq.ExpressionConverter.TranslateExpression(Expression linq) at System.Data.Objects.ELinq.ExpressionConverter.Convert() at System.Data.Objects.ELinq.ELinqQueryState.GetExecutionPlan(Nullable 1 forMergeOption( at System.Data.Objects.ObjectQuery 1.GetResults(Nullable 1forMergeOption( at System.Data.Objects.ObjectQuery 1.System.Collections.Generic.IEnumerable<T>.GetEnumerator() at System.Data.Entity.Internal.Linq.InternalQuery 1.GetEnumerator(( at System.Data.Entity.Infrastructure.DbQuery 1.System.Collections.Generic.IEnumerable<TResult>.GetEnumerator() at System.Collections.Generic.List 1..ctor(IEnumerable 1 collection) at System.Linq.Enumerable.ToList[TSource](IEnumerable 1 source( at MvcMusicStore.Controllers.StoreManagerController.Index(String sortOrder( in

尝试此代码时

var albums = db.Albums.Include(a => a.Genre).Include(a => a.Artist);
            switch (sortOrder) {
                case "Artist desc":
                    albums = albums.OrderByDescending(o => o.Artist);
                    break;
                case "Title desc":
                    albums = albums.OrderByDescending(o => o.Title);
                    break;
                case "Title":
                    albums = albums.OrderBy(o => o.Title);
                    break;
                case "Genre desc":
                    albums = albums.OrderByDescending(o => o.Genre);
                    break;
                case "Genre":
                    albums = albums.OrderBy(o => o.Genre);
                    break;
                default:
                    albums = albums.OrderBy(o => o.Artist);
                    break;
            }
            return View(albums.ToList());

我找不到排序的方法。

你说的是.Include(a => a.Artist);.OrderByDescending(o => o.Artist);。如果您尝试按对象排序Artist这将解释您的错误。 你可能想要这样的东西:

albums = albums.OrderByDescending(o => o.Artist.Name);

我假设Artist具有String属性Name,但也许它被称为不同的东西。Genre也是如此:

albums = albums.OrderByDescending(o => o.Genre.Name);

通常,您需要按基元值而不是整个对象进行排序。

相关内容

  • 没有找到相关文章

最新更新