首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >LINQ查询连接表的多个orderby

LINQ查询连接表的多个orderby
EN

Stack Overflow用户
提问于 2018-06-08 06:01:53
回答 1查看 244关注 0票数 0

我有以下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();

它运行得很好,但它不是使用ThenBy ThenBy(g => g.C.OrderBy(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
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-06-08 13:52:17

你不想对主列表进行排序,我想你是在寻找一种在外部列表中对内部列表进行排序的方法。因此,下面的代码将为您完成此操作:

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();
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50750755

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档