【发布时间】:2021-10-03 13:49:55
【问题描述】:
所以我想订购一个嵌套对象, 这个“someObject”对象在其内部具有“Values”对象。 如何通过 orderBy 对这个嵌套对象进行排序。
mySendendId=11
someObject=
Id=1
Values===
Id=10 numeric =20 text=null
Id=11 numeric =1.4 text=null
Id=12 numeric =32 text=null
Id=13 numeric=null text=”abcde”
Id=2
Values===
Id=10 numeric =21 text=null
Id=11 numeric =1.2 text=null
Id=12 numeric =33 text=null
Id=13 numeric=null text=”bcde”
Id=3
Values===
Id=10 numeric =22 text=null
Id=11 numeric =1.3 text=null
Id=12 numeric =34 text=null
Id=13 numeric=null text=”cde”
someObject =1,2,3
orderedObject = someObject.orderBy(a=>a.Values.(b=>b.numeric).Where(b=>b.Id= =mySendendId))
orderedObject =2,3,1
我已经试过了
orderedObject= someObject.OrderBy(a => a.Id).Where(a => a.Values.OrderBy(b => b.Numeric).Any(b => b.Id == mySenedId));
但我只是返回了没有排序的原始对象。
【问题讨论】:
-
我是否正确理解您希望通过匹配“mySendendId”的子 ID 的“数字”字段来排序您的根 ID?
-
另外,minimal reproducible sample 会有所帮助。
-
mySendedId 是 values.ID 的值,如果 mySendedId 和 values.ID 相等,则使用 values.numeric 对 someObject 进行排序
标签: c# linq sorting nested nested-object