简体   繁体   English

LINQ查询联接表的多个排序

[英]LINQ Query multiple orderby of joined tables

I have following LinQ query 我有以下LinQ查询

   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();

which runs fine, but it is not doing second orderby using ThenBy ThenBy(g => gCOrderBy(c => c.CCode) What am I missing? 可以正常运行,但是通过使用ThenBy ThenBy(g => gCOrderBy(c => c.CCode)不能进行第二次ThenBy(g => gCOrderBy(c => 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

You didn't want to order the main list, you are looking for a way to order inner list inside of outer one, I think. 我想,您不想订购主列表,而是在寻找一种在外部列表内部订购内部列表的方法。 So below code will do it for you: 因此,以下代码将为您完成此任务:

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();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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