Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
367 views
in Technique[技术] by (71.8m points)

linq - 按子集合属性对集合进行排序(Sort collection by child collection property)

In this code I include a child collection using LinQ (MenuSub):

(在此代码中,我包括使用LinQ(MenuSub)的子集合:)

public async Task<IEnumerable<Menus>> GetMenus(string modulo)
    {
        var result = this.context.Set<Menus>()
            .Include(det => det.MenusSub)
            .Where(e => e.Modulo.Equals(modulo))
            .OrderBy(s => s.Orden);                

        return await result.ToListAsync();
    }

This works just fine.

(这样很好。)

My question is:

(我的问题是:)

How do I Sort MenuSub collection by the orden property after sorting by orden property of the parent?

(在按父项的orden属性排序后,如何按orden属性对MenuSub集合进行排序?)

I try using the thenBy statement like this:

(我尝试使用thenBy语句,如下所示:)

public async Task<IEnumerable<Menus>> GetMenus(string modulo)
    {
        var result = this.context.Set<Menus>()
            .Include(det => det.MenusSub)
            .Where(e => e.Modulo.Equals(modulo))
            .OrderBy(s => s.Orden).ThenBy(s => s.MenusSub.OrderBy(p => p.Orden));           

        return await result.ToListAsync();
    }

But i get this error:

(但是我得到这个错误:)

"Message": "Failed to compare two elements in the array.", "StackTrace": " at System.Collections.Generic.GenericArraySortHelper 1.Sort(T[] keys, Int32 index, Int32 length, IComparer 1 comparer)\r\n at System.Array.Sort[T](T[] array, Int32 index, Int32 length, IComparer 1 comparer)\r\n at System.Linq.EnumerableSorter 1.Sort(TElement[] elements, Int32 count)\r\n at System.Linq.OrderedEnumerable 1.GetEnumerator()+MoveNext()\r\n at System.Linq.OrderedAsyncEnumerable 2.MoveNextCore(CancellationToken cancellationToken)\r\n at System.Linq.AsyncEnumerable.AsyncIterator 1.MoveNext(CancellationToken cancellationToken)\r\n at Microsoft.EntityFrameworkCore.Query.EntityQueryModelVisitor.AsyncSelectEnumerable 2.AsyncSelectEnumerator.MoveNext(CancellationToken cancellationToken)\r\n at System.Linq.AsyncEnumerable.SelectEnumerableAsyncIterator 2.MoveNextCore(CancellationToken cancellationToken)\r\n at System.Linq.AsyncEnumerable.AsyncIterator 1.MoveNext(CancellationToken cancellationToken)\r\n at Micr

(“ Message”:“无法比较数组中的两个元素。”,“ StackTrace”:“位于System.Collections.Generic.GenericArraySortHelper 1.Sort(T[] keys, Int32 index, Int32 length, IComparer 1比较器)\ r \ n位于System.Array.Sort [T](T []数组,Int32索引,Int32长度,IComparer 1 comparer)\r\n at System.Linq.EnumerableSorter 1.Sort(TElement []元素,Int32计数)\ r \ n在System.Linq.OrderedEnumerable 1.GetEnumerator()+MoveNext()\r\n at System.Linq.OrderedAsyncEnumerable 2.MoveNextCore(CancellationToken cancellingToken)\ r \ n在System.Linq.AsyncEnumerable.AsyncIterator 1.MoveNext(CancellationToken cancellationToken)\r\n at Microsoft.EntityFrameworkCore.Query.EntityQueryModelVisitor.AsyncSelectEnumerable cancelledToken 1.MoveNext(CancellationToken cancellationToken)\r\n at Microsoft.EntityFrameworkCore.Query.EntityQueryModelVisitor.AsyncSelectEnumerable 2.AsyncSelectEnumerator.MoveNext(CancellationToken cancellingToken)\ r \ n在System.Linq.AsyncEnumerable.SelectEnumerableAsyncIterator 2.MoveNextCore(CancellationToken cancellationToken)\r\n at System.Linq.AsyncEnumerable.AsyncIterator 1.MoveNext(CancellationToken cancellingToken)\ r \ n在Micr)

osoft.EntityFrameworkCore.Query.Internal.AsyncLinqOperatorProvider.ExceptionInterceptor 1.EnumeratorExceptionInterceptor.MoveNext(CancellationToken cancellationToken)\r\n at System.Linq.AsyncEnumerable.Aggregate_[TSource,TAccumulate,TResult](IAsyncEnumerable 1 source, TAccumulate seed, Func 3 accumulator, Func 2 resultSelector, CancellationToken cancellationToken)\r\n at Ohmio.Data.MenusRepository.GetMenus(String modulo) in C:\Users\ericp\Documents\Mis Proyectos\Ohmio WEB\ohmio-web-server\OhmioData\Repositorios\MenusRepository.cs:line 41\r\n at Ohmio.Servicios.MenusService.GetMenus(String modulo) in C:\Users\ericp\Documents\Mis Proyectos\Ohmio WEB\ohmio-web-server\OhmioServicios\MenusService.cs:line 26\r\n at Ohmio.Api.Controladores.MenusController.GetMenus(String modulo) in C:\Users\ericp\Documents\Mis Proyectos\Ohmio WEB\ohmio-web-server\OhmioWEBAPINetCore\Controladores\MenusController.cs:line 26\r\n at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.InvokeActionMethodAsync

(osoft.EntityFrameworkCore.Query.Internal.AsyncLinqOperatorProvider.ExceptionInterceptor 1.EnumeratorExceptionInterceptor.MoveNext(CancellationToken cancellationToken)\r\n at System.Linq.AsyncEnumerable.Aggregate_[TSource,TAccumulate,TResult](IAsyncEnumerable 1种源,TAccumulate种子,Func键3 accumulator, Func 2 resultSelector,CancellationToken cancelestToken)\ r \ n位于C:\ Users \ ericp \ Documents \ Mis Proyectos \ Ohmio WEB \ ohmio-web-server \ OhmioData \ Repositorios \中的Ohmio.Data.MenusRepository.GetMenus(字符串模) C:\ Users \ ericp \ Documents \ Mis Proyectos \ Ohmio WEB \ ohmio-web-server \ OhmioServicios \ MenusService.cs中Ohmio.Servicios.MenusService.GetMenus(字符串模)处的MenusRepository.cs:行41 \ r \ n: C:\ Users \ ericp \ Documents \ Mis Proyectos \ Ohmio WEB \ ohmio-web-server \ OhmioWEBAPINetCore \ Controladores \ MenusController.cs中Ohmio.Api.Controladores.MenusController.GetMenus(字符串模)处的第26 \ r \ n行: Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.InvokeActionMethodAsync的第26行\ r \ n)

()\r\n at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.InvokeNextActionFilterAsync()\r\n at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.Rethrow(ActionExecutedContext context)\r\n at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted)\r\n at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.InvokeInnerFilterAsync()\r\n at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeNextExceptionFilterAsync()"

(()\ r \ n在Microsoft.AspNetCore.Mvc.Internal.ControllerActionActionInvoker.InvokeNextActionFilterAsync()\ r \ n在Microsoft.AspNetCore.Mvc.Internal.ControllerActionActionInvoker.Rethrow(ActionExecutedContext context)\ r \ n在Microsoft.AspNetCore.Mvc .Internal.ControllerActionActionInvoker.Next(状态和下一个,作用域和范围,对象和状态,布尔值和isCompleted)\ r \ n在Microsoft.AspNetCore.Mvc.Internal.ControllerActionActioninvoker.InvokeInnerFilterAsync()\ r \ n在Microsoft.AspNetCore.Mvc.Internal。 ResourceInvoker.InvokeNextExceptionFilterAsync()“)

Any ideas?

(有任何想法吗?)

thanks!

(谢谢!)

  ask by ericpap translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...