通过LINQ中的孩子的属性订购父对象 [英] Order a parent object by a child's property in LINQ

查看:51
本文介绍了通过LINQ中的孩子的属性订购父对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在LINQ中按子对象排序?

How do I order by child objects in LINQ?

A,B和C类.A具有B的集合,B具有C的集合.我想通过C的Ordinal(int)属性对对象A进行排序.

Classes A, B, and C. A has a collection of B and B has a collection of C. I want to order object A by the Ordinal (int) property of C.

var query = from a in db.A
        orderby a.Bs.OrderBy(x=> x.C.Ordinal)   <--- ??
        select a;

我似乎无法弄清楚orderby语句.

I can't seem to figure out the orderby statement for this.

对不起,我的原始说法不正确:

Sorry, my original statement was incorrect:

A有一个B的集合,并且B拥有一个C对象. C不是集合.

A has a collection of B and B holds a C object. C is not a collection.

每个A都应在C的序数属性上排序.

Each A should be ordered on C's ordinal property.

另一个编辑/解决方案:

ANOTHER EDIT/Solution:

我最终在显示期间对A中的每个B集合在客户端上执行了.OrderBy(b => b.C.Ordinal).无论如何,结果都更好,因为我可以让客户端按他们需要的顺序进行排序,而不是将其嵌入到我的存储库中.

I ended up doing an .OrderBy(b=>b.C.Ordinal) on the client for each B collection in A during display. That turned out better anyway since I can let the client order by anything they need to, instead of embedding that in my repository.

推荐答案

您需要计算出您感兴趣的哪个 C.

You need to work out which C you're interested in.

从现实角度考虑这一点-如果您要按父母的孩子年龄来订购父母,您会考虑到哪个孩子的年龄?如果您有一个父母带1和5岁的孩子,以及一个父母带2和4岁的孩子,哪个应该优先?

Think of this in terms of real things - if you're trying to order parents by their children's ages, which child's age to you take into account? If you have one parent with kids of ages 1 and 5, and one parent with kids of ages 2 and 4, which should come first?

这篇关于通过LINQ中的孩子的属性订购父对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆