首页 > 其他分享 >排序用法

排序用法

时间:2022-08-24 13:59:03浏览次数:64  
标签:OrderBy SchoolId Name st StudentId 用法 sc 排序

1.基础排序

 var list = db.Queryable<Student>().OrderBy("StudentId DESC").ToList();

//SELECT `StudentId`,`Name`,`SchoolId` FROM `Student` ORDER BY StudentId DESC

2.表达式排序

var list = db.Queryable<Student, School>((st, sc) => new JoinQueryInfos(JoinType.Left, st.SchoolId == sc.SchoolId))
    .OrderBy(st => st.StudentId)
    .OrderBy((st, sc) => sc.SchoolId, OrderByType.Desc)
    .Select<ViewModelStudent>().ToList();
    
//SELECT st.`StudentId` AS `StudentId`,st.`Name` AS `Name`,st.`SchoolId` AS `SchoolId`,sc.`SchoolName` AS `SchoolName` FROM `Student` st
   Left JOIN `School` sc ON( `st`.`SchoolId` = `sc`.`SchoolId` )  
   ORDER BY `st`.`StudentId` ASC,`sc`.`SchoolId` DESC

Student和School结合的实体类
ViewModelStudent表中的数据

3.表达式连写

3.1 升序

var list = db.Queryable<Student>().OrderBy(it => new { it.StudentId,it.SchoolId }).ToList();

//SELECT `StudentId`,`Name`,`SchoolId` FROM `Student` ORDER BY `StudentId` ASC,`SchoolId` ASC

3.2 降序

var list = db.Queryable<Student>().OrderBy(it=>new { it.StudentId,SchoolId = SqlFunc.Desc(it.SchoolId)}).ToList();

//SELECT `StudentId`,`Name`,`SchoolId` FROM `Student` ORDER BY `StudentId`,`SchoolId` DESC

4.随机排序取10条

var list = db.Queryable<Student().OrderBy(it=>SqlFunc.GetRandom()).Take(3).ToList();

//SELECT `StudentId`,`Name`,`SchoolId` FROM `Student`    ORDER BY rand() ASC LIMIT 0,3

SqlFunc.GetRandom:随机数函数,不理解的可以用下面语句测试查看

select *,rand() as random from student  order by random

5.OrderByIF

//当条件IsOrderBy == true,OrderBy才生效
OrderByIF(IsOrderBy, it=>it.Id) 

文档参考:排序用法

标签:OrderBy,SchoolId,Name,st,StudentId,用法,sc,排序
From: https://www.cnblogs.com/DotNeter-Hpf/p/16619620.html

相关文章

  • PHP多维数组按照某个字段进行排序
    作为开发人员,您可能会遇到这种情况,即数据库中有一个按特定顺序获取的数据列表,但您希望在前端显示这些项目时安装期中某一个字段进行排序。比如数组:$mylist=array(arra......
  • c# Dictionary.TryGetValue()的用法
    官方解释 上面解释:1)TryGetValue是根据key返回相应的数据到value,如果没有key则返回默认值到value;2)这个方法的返回是bool值,如果dictionary里有存在相应的k......
  • 简述JS中forEach()、map()、every()、some()和filter()的用法
    在文章开头,先问大家一个问题:在Javascript中,如何处理数组中的每一项数据?有人可能会说,这还不简单,直接一个for循环遍历一下就好了。是的,确实,这是最常见的做法。但是,除......
  • oracle number类型用法
    1.number类型  number类型是一个可变长度的数据类型,使用四舍五入实现;  既可以存储整数,也可以存储小数。2.具体语法number[(p[,s])]NUMBER类型可以用来存储0、......
  • 【组合数学】错位排序
    错位排序:编号为\(1\)到\(n\)的球装进编号\(1\)到\(n\)的盒子里,问每个球与它所在的盒子编号都不同的方案数。公式:设\(D_n\)表示\(n\)个球的方案数,则:\(D_n=(n-1......
  • 链表冒泡排序
    publicListNodesortList(ListNodehead){ListNodeheadpre=newListNode();headpre.next=head;intlength=0;while(head!=null){......
  • 经常使用的一些函数及用法
    foriinrange(0,3,2)#i从0到3,每2个数取一次值input("pleaseinputtheage")#输入框,里面填提示信息int("123")#把......
  • 序列化与反序列化的用法
    usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Runtime.Serialization.Formatters.Binary;usingSystem.IO;n......
  • Java中枚举配合switch语句用法-2022新项目
    一、业务场景项目开发中经常会遇到多条件判断的情况,如果判断条件少的话使用if/elseif/else还比较好处理,如果判断条件多的话,则在使用这种语句就不太合适。如果是自定......
  • 数组排序方法sort---案例
    <template><divclass="Leading"><h2>人员列表</h2><inputtype="text"placeholder="请输入名字"v-model="keyword"><br><!--{{keyword}}-->......