C# LINQ查询联接表的多个orderby
我有以下LinQ查询C# LINQ查询联接表的多个orderby,c#,linq,C#,Linq,我有以下LinQ查询 var CGTABLE = (from cg in DbContext.CGTABLE join tcg in DbContext.TCGTABLE on new { cg.CGroupId } equals new { tcg.CGroupId }
var CGTABLE = (from cg in DbContext.CGTABLE
join tcg in DbContext.TCGTABLE on new { cg.CGroupId } equals new { tcg.CGroupId }
where tcg.TId == TId
select new {
CGroupId = cg.CGroupId,
CGroupCode = cg.CGroupCode,
Description = cg.Description,
C = cg.C,
DisplayOrder = cg.DisplayOrder
}).ToList();
CGTABLE = CGTABLE.OrderBy(g => g.DisplayOrder).ThenBy(g => g.C.OrderBy(c => c.CCode)).ToList();
运行良好,但它不是通过使用ThenBy ThenByg=>g.C.OrderByc=>C.CCode进行二次排序,我缺少什么
Sample data for better understanding.
Data in Tables
2
1
2
4
3
1
4
5
2
1
3
3
1
Should output after both outer and inner list ordered by
1
1
2
3
4
2
1
2
4
5
3
1
3
But Currently it is showing
1
4
5
2
1
2
1
2
4
3
3
3
1
我想,你不想在主列表中排序,你是在寻找一种将内部列表排序到外部列表中的方法。 因此,下面的代码将为您提供:
var CGTABLE = (
from cg in DbContext.CGTABLE
join tcg in DbContext.TCGTABLE on new { cg.CGroupId } equals new { tcg.CGroupId }
where tcg.TId == TId
select new {
CGroupId = cg.CGroupId,
CGroupCode = cg.CGroupCode,
Description = cg.Description,
C = cg.C.OrderBy(x => x.CCode),
DisplayOrder = cg.DisplayOrder
}).ToList();
CGTABLE = CGTABLE.OrderBy(g => g.DisplayOrder).ToList();
您是否尝试过CGTABLE=CGTABLE.OrderByg=>g.DisplayOrder.OrderByc=>c.CCode.ToList;?不起作用,因为CCode列位于C表中,C表是一个单独的表,C表是记录的集合。我正在努力理解它的作用。然后BYG=>g.C.OrderByc=>C.CCode就是要这样做的。您正在按g.C.OrderByc=>C.CCode进行排序,但这将是一个IOrderedEnumerable,我不确定如何根据其中一个进行排序。。。所以我认为无论你想做什么,你都使用了错误的语法,但我不确定你想做什么…我把三个表连接在一起。我正在对不同表中的两列进行排序。它返回所有必需的记录,但不按顺序执行。这里是最接近上述脚本的查询,从CGTABLE中选择*作为CG内部联接C作为C在CG.CGroupId=C.CGroupId=C.CGroupId内部联接TCGTABLE作为TCG在C.CGroupId=TCG.CGroupId中TCG.TId=2 order BY CG.DisplayOrder,C这是问题所在,我要按顺序列出。顶级父级以及这些父级中的内部列表。将使用数据更新问题以获得更多许可。