linq to sql Distinct and orderby

前端 未结 3 459
陌清茗
陌清茗 2020-12-20 10:54
var result = table1.Join(table2, o => o.ProgramID, t => t.ProgramID, (o, t) => new { o.ProgramID, t.Program })
         .OrderBy(t => t.Program)
                 


        
相关标签:
3条回答
  • 2020-12-20 11:33

    I don't know if it will help, but you can try something like this;

    var result = (from o in table1
                  join t in table2 on o.ProgramID equals t.ProgramID
                  orderby t.Program
                  select new { o.ProgramID, t.Program }).Distinct();
    
    0 讨论(0)
  • 2020-12-20 11:33

    I tried this and that works:

    var result = (from o in table1
                  join t in table2 on o.ProgramID equals t.ProgramID
                  select new { o.ProgramID, t.Program })
                  .Distinct().OrderBy(t => t.Program)
                             .ThenBy(t => t.ProgramName)
                             .ThenBy(t => t.Description); 
    

    First you do Distinct and then OrderBy, then OrderBy works.

    0 讨论(0)
  • 2020-12-20 11:39

    Profile the two queries, comparing stats-IO and the actual execution plan. It is entirely possible that it makes zero difference to the SQL server.

    If you really want known TSQL, use ExecuteQuery-of-T and pass in the TSQL yourself. Maybe include some lock hints too (most commonly: NOLOCK)

    0 讨论(0)
提交回复
热议问题