简体   繁体   中英

How to OrderBy using a child collection in lambda expression

I have a query using lambda expressions and I want to be able to sort from a column in a table that is a child collection of a parent table. The query looks like this:

 var query = ctx.Timelines.Include("Rule.DocumentRules").Where(...).OrderBy(o => o.Rule.DocumentRules.OrderBy(t => t.SortOrder));

The SortOrder column resides 3 levels deep under the Timelines Entity and I don't know which extension to use to access it. When I use the code above, I get an error "DbSortClause expressions must have a type that is order comparable. Parameter name: key". I get the same error if I use a Select extension instead of the 2nd OrderBy. Does anyone know how I can sort by this column? This "SortOrder" column is not a primary or foreign key.

Thanks

I know this is an old thread, but I came across this post as I was having the same issue and since it didn't have an answer I thought I would share my solution in the event it helped someone else.

Let's say you have a requirement to show an actual parent (ie...mom/dad) and their children but you want to show the children's names in alphabetical order in the report. If they are not in the database in the right order, you must order them by the "Name" field on the child table to display them based on the requirement.

Simple T-SQL example that works as expected:

select * from ParentTable p
inner join ChildTable c on c.ParentId = p.ParentId
where p.ParentId = 1
order by c.Name

I had tried to accomplish this the same way the original poster did (shown below) and got a "DbSortClause expressions must have a type that is order comparable" exception.

.OrderBy(x => x.ParentTable.ChildTable.OrderBy(y => y.Name))

I'm using projection so this may not work for all, but what I found is that if I did the OrderBy inside the projection of the child list it worked as I wanted it too.

Children = x.ParentTable.ChildTable.OrderBy(y => y.Name).Select(aa => new ChildTableModel

There may very well be a better/different way to accomplish this, but this did what I wanted so I thought I would share.

It looks like you're trying to sort by multiple child items, which doesn't really make sense to the compiler. If there is indeed a 1 to 1 relationship between those two entities you could try something like

var query = ctx.Timelines.Include("Rule.DocumentRules").Where(...).OrderBy(o => o.Rule.DocumentRules.First().SortOrder));

this could result in a null exception depending on how your database is set up.

I've faced same issue when was trying to sort by column in related entities. The only one solution that works well was to use Entity SQL. Here is a simple example of using ESQL query which returns ObjectQuery: Converting ESQL to LINQ to Entities. Sort by related entities .

Hopefully this will help.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM