本文介绍了LINQ排序依据名称ThenBy ChildrenCollection.Name的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有LINQ没有办法做一个排序依据,然后使用父对象的孩子做二次订货做的ThenBy一个ThenBy?



<$ P $ 。p> _repository.GetActiveDepartmentGroupsWithDepartments()排序依据(C => c.DepartmentGroupName).ThenBy(C => c.Departments.OrderBy(D => d.DepartmentName))

在上述情况下,c.Departments是一个EntityCollection。



BTW:当我尝试了上面,然后在其上做了ToList(),我得到这个错误:

  DbSortClause表达式必须有一个类型是顺序相媲美。 
参数名:关键

在事先的任何帮助或指导谢谢


解决方案

好像你正在试图获得通过组,然后部门名称下令各部门的列表。如果是这样,那么你可能想要做这样的事情:

 在_repository.GetActiveDepartmentGroupsWithDepartments从C变种解析度=()$ B从D $ b在c.Departments 
排序依据c.DepartmentGroupName,d.DepartmentName
选择D组;

或在方法的语法:

  VAR解析度= _repository.GetActiveDepartmentGroupsWithDepartments()
.SelectMany(C => c.Departments,(C,D)=>新建{C,D})
.OrderBy(X => xcDepartmentGroupName)
.ThenBy(X => xdDepartmentName)
。选择(X => XD);


Is there any way in LINQ to do an OrderBy and then do a ThenBy with the ThenBy using the children of the parent object to do the secondary ordering?

_repository.GetActiveDepartmentGroupsWithDepartments().OrderBy(c => c.DepartmentGroupName).ThenBy(c => c.Departments.OrderBy(d => d.DepartmentName))

In the above case, c.Departments is an EntityCollection.

BTW: When I try the above and then do a ToList() on it, I get this error:

DbSortClause expressions must have a type that is order comparable.
Parameter name: key

Thanks in advance for any help or guidance.

解决方案

It seems like you're trying to get a list of all departments ordered by group then department name. If so, then you probably want to do something like this:

var res = from c in _repository.GetActiveDepartmentGroupsWithDepartments()
          from d in c.Departments
          orderby c.DepartmentGroupName, d.DepartmentName
          select d;

Or in method syntax:

var res = _repository.GetActiveDepartmentGroupsWithDepartments()
                     .SelectMany(c => c.Departments, (c,d) => new { c, d })
                     .OrderBy(x => x.c.DepartmentGroupName)
                     .ThenBy(x => x.d.DepartmentName)
                     .Select(x => x.d);

这篇关于LINQ排序依据名称ThenBy ChildrenCollection.Name的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-15 06:24