You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

569 lines
27 KiB

2 years ago
  1. using OrmTest.Models;
  2. using SqlSugar;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. namespace OrmTest.Demo
  9. {
  10. public class Query : DemoBase
  11. {
  12. public static void Init()
  13. {
  14. Easy();
  15. Page();
  16. Where();
  17. Join();
  18. Funs();
  19. Select();
  20. Ado();
  21. Group();
  22. Sqlable();
  23. Tran();
  24. StoredProcedure();
  25. Enum();
  26. Simple();
  27. Async();
  28. Subqueryable();
  29. SqlQueryable();
  30. }
  31. private static void SqlQueryable()
  32. {
  33. var db = GetInstance();
  34. var list = db.SqlQueryable<Student>("select * from student").ToPageList(1, 2);
  35. }
  36. private static void Subqueryable()
  37. {
  38. var db = GetInstance();
  39. var i = 0;
  40. var getAll11 = db.Queryable<Student>().Where(it => SqlFunc.Subqueryable<School>().Where(s => s.Id == it.Id).Max(s=>s.Id)==i).ToList();
  41. var getAll12 = db.Queryable<Student>().Where(it => SqlFunc.Subqueryable<School>().Where(s => s.Id == it.Id).Max(s => s.Id) == 1).ToList();
  42. var getAll7 = db.Queryable<Student>().Where(it => SqlFunc.Subqueryable<School>().Where(s => s.Id == it.Id).Any()).ToList();
  43. var getAll9 = db.Queryable<Student>().Where(it => SqlFunc.Subqueryable<School>().Where(s => s.Id == it.Id).Count()==1).ToList();
  44. var getAll10 = db.Queryable<Student>().Where(it => SqlFunc.Subqueryable<School>().Where(s => s.Id == it.Id).OrderBy(s=>s.Id).Select(s=>s.Id) == 1).ToList();
  45. var getAll14 = db.Queryable<Student>().Where(it => SqlFunc.Subqueryable<School>().Where(s => s.Id == it.Id).OrderByDesc(s => s.Id).Select(s => s.Id) == 1).ToList();
  46. var getAll8= db.Queryable<Student>().Where(it => SqlFunc.Subqueryable<School>().Where(s => s.Id == it.Id).Where(s=>s.Name==it.Name).NotAny()).ToList();
  47. var getAll1 = db.Queryable<Student>().Where(it => it.Id == SqlFunc.Subqueryable<School>().Where(s => s.Id == it.Id).Select(s => s.Id)).ToList();
  48. var getAll2 = db.Queryable<Student, School>((st, sc) => new object[] {
  49. JoinType.Left,st.Id==sc.Id
  50. })
  51. .Where(st => st.Id == SqlFunc.Subqueryable<School>().Where(s => s.Id == st.Id).Select(s => s.Id))
  52. .ToList();
  53. var getAll3 = db.Queryable<Student, School>((st, sc) => new object[] {
  54. JoinType.Left,st.Id==sc.Id
  55. })
  56. .Select(st =>
  57. new
  58. {
  59. name = st.Name,
  60. id = SqlFunc.Subqueryable<School>().Where(s => s.Id == st.Id).Select(s => s.Id)
  61. })
  62. .ToList();
  63. var getAll4 = db.Queryable<Student>().Select(it =>
  64. new
  65. {
  66. name = it.Name,
  67. id = SqlFunc.Subqueryable<School>().Where(s => s.Id == it.Id).Select(s => s.Id)
  68. }).ToList();
  69. var getAll5 = db.Queryable<Student>().Select(it =>
  70. new Student
  71. {
  72. Name = it.Name,
  73. Id = SqlFunc.Subqueryable<School>().Where(s => s.Id == it.Id).Select(s => s.Id)
  74. }).ToList();
  75. }
  76. private static void Async()
  77. {
  78. var db = GetInstance();
  79. var list = db.Queryable<Student>().Where(it => it.Id == 1).SingleAsync();
  80. list.Wait();
  81. var list2 = db.Queryable<Student>().SingleAsync(it => it.Id == 1);
  82. list2.Wait();
  83. var list3 = db.Queryable<Student>().Where(it => it.Id == 1).ToListAsync();
  84. list3.Wait();
  85. var list4 = db.Queryable<Student>().Where(it => it.Id == 1).ToPageListAsync(1, 2);
  86. list4.Wait();
  87. }
  88. private static void Simple()
  89. {
  90. //SqlSugarClient
  91. var db = GetInstance();
  92. var student1 = db.Queryable<Student>().InSingle(1);
  93. //get SimpleClient
  94. var sdb = db.GetSimpleClient();
  95. var student2 = sdb.GetById<Student>(1);
  96. sdb.DeleteById<Student>(1);
  97. sdb.Insert(new Student() { Name = "xx" });
  98. sdb.Update<Student>(it => new Student { Name = "newvalue" }, it => it.Id == 1);//only update name where id=1
  99. sdb.Update(new Student() { Name = "newavalue", Id = 1 });//update all where id=1
  100. //SimpleClient Get SqlSugarClient
  101. var student3 = sdb.FullClient.Queryable<Student>().InSingle(1);
  102. }
  103. private static void StoredProcedure()
  104. {
  105. var db = GetInstance();
  106. //1. no result
  107. db.Ado.UseStoredProcedure(() =>
  108. {
  109. string spName = "sp_help";
  110. var getSpReslut = db.Ado.SqlQueryDynamic(spName, new { objname = "student" });
  111. });
  112. //2. has result
  113. var result = db.Ado.UseStoredProcedure<dynamic>(() =>
  114. {
  115. string spName = "sp_help";
  116. return db.Ado.SqlQueryDynamic(spName, new { objname = "student" });
  117. });
  118. //2. has output
  119. object outPutValue;
  120. var outputResult = db.Ado.UseStoredProcedure<dynamic>(() =>
  121. {
  122. string spName = "sp_school";
  123. var p1 = new SugarParameter("@p1", "1");
  124. var p2 = new SugarParameter("@p2", null, true);//isOutput=true
  125. var dbResult = db.Ado.SqlQueryDynamic(spName, new SugarParameter[] { p1, p2 });
  126. outPutValue = p2.Value;
  127. return dbResult;
  128. });
  129. //3
  130. var dt = db.Ado.UseStoredProcedure().GetDataTable("sp_school", new { p1 = 1, p2 = 2 });
  131. var p11 = new SugarParameter("@p1", "1");
  132. var p22 = new SugarParameter("@p2", null, true);//isOutput=true
  133. //4
  134. var dt2 = db.Ado.UseStoredProcedure().SqlQuery<School>("sp_school", p11, p22);
  135. }
  136. private static void Tran()
  137. {
  138. var db = GetInstance();
  139. var x = db.Insertable(new Student() { CreateTime = DateTime.Now, Name = "tran" }).ExecuteCommand();
  140. //1. no result
  141. var result = db.Ado.UseTran(() =>
  142. {
  143. var beginCount = db.Queryable<Student>().ToList();
  144. db.Ado.ExecuteCommand("delete student");
  145. var endCount = db.Queryable<Student>().Count();
  146. throw new Exception("error haha");
  147. });
  148. var count = db.Queryable<Student>().Count();
  149. //2 has result
  150. var result2 = db.Ado.UseTran<List<Student>>(() =>
  151. {
  152. return db.Queryable<Student>().ToList();
  153. });
  154. //3 use try
  155. try
  156. {
  157. db.Ado.BeginTran();
  158. db.Ado.CommitTran();
  159. }
  160. catch (Exception)
  161. {
  162. db.Ado.RollbackTran();
  163. throw;
  164. }
  165. }
  166. private static void Group()
  167. {
  168. var db = GetInstance();
  169. var list = db.Queryable<Student>()
  170. .GroupBy(it => it.Name)
  171. .GroupBy(it => it.Id).Having(it => SqlFunc.AggregateAvg(it.Id) > 0)
  172. .Select(it => new { idAvg = SqlFunc.AggregateAvg(it.Id), name = it.Name }).ToList();
  173. var list1 = db.Queryable<Student>()
  174. .GroupBy(it => it.Name)
  175. .GroupBy(it => it.Id).Having(it => SqlFunc.AggregateAvg(it.Id) > 0)
  176. .Select(it => new { idAvg = SqlFunc.AggregateAvg(it.Id), name = it.Name }).ToSql();
  177. var list2 = db.Queryable<Student>()
  178. .GroupBy(it => new { it.Id, it.Name }).Having(it => SqlFunc.AggregateAvg(it.Id) > 0)
  179. .Select(it => new { idAvg = SqlFunc.AggregateAvg(it.Id), name = it.Name }).ToList();
  180. var list7 = db.Queryable<Student>()
  181. .GroupBy(it => new { it.Id, it.Name }).Having(it => SqlFunc.AggregateAvg(it.Id) > 0)
  182. .Select(it => new { idAvg = SqlFunc.AggregateCount(1), name = it.Name }).ToSql();
  183. //SQL:
  184. //SELECT AVG([Id]) AS[idAvg], [Name] AS[name] FROM[Student] GROUP BY[Name],[Id] HAVING(AVG([Id]) > 0 )
  185. // group id,name take first
  186. var list3 = db.Queryable<Student>()
  187. .PartitionBy(it => new { it.Id, it.Name }).Take(1).ToList();
  188. var list31 = db.Queryable<Student>()
  189. .PartitionBy(it => new { it.Id, it.Name }).Take(1).Count();
  190. int count = 0;
  191. var list4 = db.Queryable<Student, School>((st, sc) => st.SchoolId == sc.Id)
  192. .PartitionBy(st => new { st.Name }).Take(2).OrderBy(st => st.Id, OrderByType.Desc).Select(st => st).ToPageList(1, 1000, ref count);
  193. var list4_sql = db.Queryable<Student, School>((st, sc) => st.SchoolId == sc.Id)
  194. .PartitionBy(st => new { st.Name }).Take(2).OrderBy(st => st.Id, OrderByType.Desc).Select(st => st).ToSql();
  195. //SqlFunc.AggregateSum(object thisValue)
  196. //SqlFunc.AggregateAvg<TResult>(TResult thisValue)
  197. //SqlFunc.AggregateMin(object thisValue)
  198. //SqlFunc.AggregateMax(object thisValue)
  199. //SqlFunc.AggregateCount(object thisValue)
  200. }
  201. private static void Ado()
  202. {
  203. var db = GetInstance();
  204. db.Ado.BeginTran();
  205. var t1 = db.Ado.SqlQuery<string>("select 'a'");
  206. var t2 = db.Ado.GetInt("select 1");
  207. var t3 = db.Ado.GetDataTable("select 1 as id");
  208. db.Ado.CommitTran();
  209. var t11 = db.Ado.SqlQuery<Student>("select * from student");
  210. //more
  211. //db.Ado.GetXXX...
  212. }
  213. public static void Easy()
  214. {
  215. var db = GetInstance();
  216. var dbTime = db.GetDate();
  217. var getAll = db.Queryable<Student>().ToList();
  218. var getAllOrder = db.Queryable<Student>().OrderBy(it => it.Id).OrderBy(it => it.Name, OrderByType.Desc).ToList();
  219. var getId = db.Queryable<Student>().Select(it => it.Id).ToList();
  220. var getNew = db.Queryable<Student>().Where(it => it.Id == 1).Select(it => new { id = SqlFunc.IIF(it.Id == 0, 1, it.Id), it.Name, it.SchoolId }).ToList();
  221. var getAllNoLock = db.Queryable<Student>().With(SqlWith.NoLock).ToList();
  222. var getByPrimaryKey = db.Queryable<Student>().InSingle(2);
  223. var getSingleOrDefault = db.Queryable<Student>().Where(it => it.Id == 1).Single();
  224. var getFirstOrDefault = db.Queryable<Student>().First();
  225. var getByWhere = db.Queryable<Student>().Where(it => it.Id == 1 || it.Name == "a").ToList();
  226. var getByFuns = db.Queryable<Student>().Where(it => SqlFunc.IsNullOrEmpty(it.Name)).ToList();
  227. var sum = db.Queryable<Student>().Select(it => it.SchoolId).ToList();
  228. var sum2 = db.Queryable<Student, School>((st, sc) => st.SchoolId == sc.Id).Sum((st, sc) => sc.Id);
  229. var isAny = db.Queryable<Student>().Where(it => it.Id == -1).Any();
  230. var isAny2 = db.Queryable<Student>().Any(it => it.Id == -1);
  231. var count = db.Queryable<Student>().Count(it => it.Id > 0);
  232. var date = db.Queryable<Student>().Where(it => it.CreateTime.Value.Date == DateTime.Now.Date).ToList();
  233. var getListByRename = db.Queryable<School>().AS("Student").ToList();
  234. var in1 = db.Queryable<Student>().In(it => it.Id, new int[] { 1, 2, 3 }).ToList();
  235. var in2 = db.Queryable<Student>().In(new int[] { 1, 2, 3 }).ToList();
  236. int[] array = new int[] { 1, 2 };
  237. var in3 = db.Queryable<Student>().Where(it => SqlFunc.ContainsArray(array, it.Id)).ToList();
  238. var group = db.Queryable<Student>().GroupBy(it => it.Id)
  239. .Having(it => SqlFunc.AggregateCount(it.Id) > 10)
  240. .Select(it => new { id = SqlFunc.AggregateCount(it.Id) }).ToList();
  241. var between = db.Queryable<Student>().Where(it => SqlFunc.Between(it.Id, 1, 20)).ToList();
  242. var getTodayList = db.Queryable<Student>().Where(it => SqlFunc.DateIsSame(it.CreateTime, DateTime.Now)).ToList();
  243. var joinSql = db.Queryable("student", "s").OrderBy("id").Select("id,name").ToPageList(1, 2);
  244. var getDay1List = db.Queryable<Student>().Where(it => it.CreateTime.Value.Hour == 1).ToList();
  245. var getDateAdd = db.Queryable<Student>().Where(it => it.CreateTime.Value.AddDays(1) == DateTime.Now).ToList();
  246. var getDateIsSame = db.Queryable<Student>().Where(it => SqlFunc.DateIsSame(DateTime.Now, DateTime.Now, DateType.Hour)).ToList();
  247. var getSqlList = db.Queryable<Student>().AS("(select * from student) t").ToList();
  248. var getUnionAllList = db.UnionAll(db.Queryable<Student>().Where(it => it.Id == 1), db.Queryable<Student>().Where(it => it.Id == 2)).ToList();
  249. var getUnionAllList2 = db.UnionAll(db.Queryable<Student>(), db.Queryable<Student>()).ToList();
  250. var test1 = db.Queryable<Student, School>((st, sc) => st.SchoolId == sc.Id).Where(st=>st.CreateTime>SqlFunc.GetDate()).Select((st, sc) => SqlFunc.ToInt64(sc.Id)).ToList();
  251. var test2 = db.Queryable<Student, School>((st, sc) => st.SchoolId == sc.Id)
  252. .Where(st =>
  253. SqlFunc.IF(st.Id > 1)
  254. .Return(st.Id)
  255. .ElseIF(st.Id == 1)
  256. .Return(st.SchoolId).End(st.Id) == 1).Select(st=>st).ToList();
  257. var test2_sql = db.Queryable<Student, School>((st, sc) => st.SchoolId == sc.Id)
  258. .Where(st =>
  259. SqlFunc.IF(st.Id > 1)
  260. .Return(st.Id)
  261. .ElseIF(st.Id == 1)
  262. .Return(st.SchoolId).End(st.Id) == 1).Select(st => st).ToSql();
  263. var test3 = db.Queryable<DataTestInfo2>().Select(it => it.Bool1).ToSql();
  264. var test4 = db.Queryable<DataTestInfo2>().Select(it => new { b=it.Bool1 }).ToSql();
  265. DateTime? result = DateTime.Now;
  266. var test5 = db.Queryable<Student>().Where(it=>it.CreateTime> result.Value.Date).ToList();
  267. }
  268. public static void Page()
  269. {
  270. var db = GetInstance();
  271. var pageIndex = 1;
  272. var pageSize = 2;
  273. var totalCount = 0;
  274. //page
  275. var page = db.Queryable<Student>().OrderBy(it => it.Id).ToPageList(pageIndex, pageSize, ref totalCount);
  276. //page join
  277. var pageJoin = db.Queryable<Student, School>((st, sc) => new object[] {
  278. JoinType.Left,st.SchoolId==sc.Id
  279. }).ToPageList(pageIndex, pageSize, ref totalCount);
  280. //top 5
  281. var top5 = db.Queryable<Student>().Take(5).ToList();
  282. //skip5
  283. var skip5 = db.Queryable<Student>().Skip(5).ToList();
  284. }
  285. public static void Where()
  286. {
  287. var db = GetInstance();
  288. //join
  289. var list = db.Queryable<Student, School>((st, sc) => new object[] {
  290. JoinType.Left,st.SchoolId==sc.Id
  291. })
  292. .Where((st, sc) => sc.Id == 1)
  293. .Where((st, sc) => st.Id == 1)
  294. .Where((st, sc) => st.Id == 1 && sc.Id == 2).ToList();
  295. //SELECT [st].[Id],[st].[SchoolId],[st].[Name],[st].[CreateTime] FROM [Student] st
  296. //Left JOIN School sc ON ( [st].[SchoolId] = [sc].[Id] )
  297. //WHERE ( [sc].[Id] = @Id0 ) AND ( [st].[Id] = @Id1 ) AND (( [st].[Id] = @Id2 ) AND ( [sc].[Id] = @Id3 ))
  298. //Where If
  299. string name = null;
  300. string name2 = "sunkaixuan";
  301. var list2 = db.Queryable<Student>()
  302. .WhereIF(!string.IsNullOrEmpty(name), it => it.Name == name)
  303. .WhereIF(!string.IsNullOrEmpty(name2), it => it.Name == name2).ToList();
  304. //join
  305. var list3 = db.Queryable<Student, School>((st, sc) => new object[] {
  306. JoinType.Left,st.SchoolId==sc.Id
  307. })
  308. .WhereIF(false, (st, sc) => sc.Id == 1)
  309. .WhereIF(false, (st, sc) => st.Id == 1).ToList();
  310. var list4 = db.Queryable<Student, School>((st, sc) => new object[] {
  311. JoinType.Left,st.SchoolId==sc.Id
  312. })
  313. .Select((st, sc) => new { id = st.Id, school = sc }).ToList();
  314. var list5 = db.Queryable<Student, School>((st, sc) => new object[] {
  315. JoinType.Left,st.SchoolId==sc.Id
  316. }).AS<Student>("STUDENT").AS<School>("SCHOOL")
  317. .Select((st, sc) => new { id = st.Id, school = sc }).ToList();
  318. var list5_sql = db.Queryable<Student, School>((st, sc) => new object[] {
  319. JoinType.Left,st.SchoolId==sc.Id
  320. }).AS<Student>("STUDENT").AS<School>("SCHOOL")
  321. .Select((st, sc) => new { id = st.Id, school = sc }).ToSql();
  322. var list6 = db.Queryable<Student, School>((st, sc) => new object[] {
  323. JoinType.Left,st.SchoolId==sc.Id
  324. }).With(SqlWith.NoLock).AS<Student>("STUDENT").AS<School>("SCHOOL")
  325. .Select((st, sc) => new { id = st.Id, school = sc }).ToList();
  326. }
  327. public static void Join()
  328. {
  329. var db = GetInstance();
  330. //join 2
  331. var list = db.Queryable<Student, School>((st, sc) => new object[] {
  332. JoinType.Left,st.SchoolId==sc.Id
  333. })
  334. .Where(st => st.Name == "jack").ToList();
  335. //join 3
  336. var list2 = db.Queryable<Student, School, Student>((st, sc, st2) => new object[] {
  337. JoinType.Left,st.SchoolId==sc.Id,
  338. JoinType.Left,st.SchoolId==st2.Id
  339. })
  340. .Where((st, sc, st2) => st2.Id == 1 || sc.Id == 1 || st.Id == 1).With(SqlWith.NoLock).ToList();
  341. //join return List<ViewModelStudent>
  342. var list3 = db.Queryable<Student, School>((st, sc) => new object[] {
  343. JoinType.Left,st.SchoolId==sc.Id
  344. }).Select((st, sc) => new ViewModelStudent { Name = st.Name, SchoolId = sc.Id }).ToList();
  345. //join Order By (order by st.id desc,sc.id desc)
  346. var list4 = db.Queryable<Student, School>((st, sc) => new object[] {
  347. JoinType.Left,st.SchoolId==sc.Id
  348. })
  349. .OrderBy(st => st.Id, OrderByType.Desc)
  350. .OrderBy((st, sc) => sc.Id, OrderByType.Desc)
  351. .Select((st, sc) => new ViewModelStudent { Name = st.Name, SchoolId = sc.Id }).ToList();
  352. //join 2
  353. var list4_1 = db.Queryable<Student, School>((st, sc) => new object[] {
  354. JoinType.Left,st.SchoolId==sc.Id&& st.Name == "jack"
  355. }).ToList();
  356. //The simple use of Join 2 table
  357. var list5 = db.Queryable<Student, School>((st, sc) => st.SchoolId == sc.Id).Select((st, sc) => new { st.Name, st.Id, schoolName = sc.Name }).ToList();
  358. //join 3 table
  359. var list6 = db.Queryable<Student, School, School>((st, sc, sc2) => st.SchoolId == sc.Id && sc.Id == sc2.Id)
  360. .Select((st, sc, sc2) => new { st.Name, st.Id, schoolName = sc.Name, schoolName2 = sc2.Name }).ToList();
  361. //join 3 table page
  362. var list7 = db.Queryable<Student, School, School>((st, sc, sc2) => st.SchoolId == sc.Id && sc.Id == sc2.Id)
  363. .Select((st, sc, sc2) => new { st.Name, st.Id, schoolName = sc.Name, schoolName2 = sc2.Name }).ToPageList(1, 2);
  364. //join 3 table page
  365. var list8 = db.Queryable<Student, School, School>((st, sc, sc2) => st.SchoolId == sc.Id && sc.Id == sc2.Id)
  366. .OrderBy(st => st.Id)
  367. .Select((st, sc, sc2) => new { st.Name, st.Id, schoolName = sc.Name, schoolName2 = sc2.Name }).ToPageList(1, 2);
  368. //In
  369. var list9 = db.Queryable<Student>("it")
  370. .OrderBy(it => it.Id)
  371. .In(it => it.Id, db.Queryable<School>().Where("it.id=schoolId").Select(it => it.Id))
  372. .ToList();
  373. //SELECT [ID],[SchoolId],[Name],[CreateTime] FROM [STudent] it WHERE [ID]
  374. //IN (SELECT [Id] FROM [School] WHERE it.id=schoolId ) ORDER BY [ID] ASC
  375. var list10 = db.Queryable<Student, School>((st, sc) => st.SchoolId == sc.Id)
  376. .In(st => st.Name, db.Queryable<School>("sc2").Where("id=st.schoolid").Select(it => it.Name))
  377. .OrderBy(st => st.Id)
  378. .Select(st => st)
  379. .ToList();
  380. //SELECT st.* FROM [STudent] st ,[School] sc WHERE ( [st].[SchoolId] = [sc].[Id] ) AND [st].[Name]
  381. //IN (SELECT [Name] FROM [School] sc2 WHERE id=st.schoolid ) ORDER BY [st].[ID] ASC
  382. var list11 = db.Queryable<Student, School>((st, sc) => st.SchoolId == sc.Id)
  383. .In(st => st.Name, db.Queryable<School>("sc2").Where(it => it.Id == 1).Where("id=st.schoolid").Select(it => it.Name))
  384. .OrderBy(st => st.Id)
  385. .Select(st => st)
  386. .ToList();
  387. var list11_sql = db.Queryable<Student, School>((st, sc) => st.SchoolId == sc.Id)
  388. .In(st => st.Name, db.Queryable<School>("sc2").Where(it => it.Id == 1).Where("id=st.schoolid").Select(it => it.Name))
  389. .OrderBy(st => st.Id)
  390. .Select(st => st)
  391. .ToSql();
  392. }
  393. public static void Funs()
  394. {
  395. var db = GetInstance();
  396. var t1 = db.Queryable<Student>().Where(it => SqlFunc.ToLower(it.Name) == SqlFunc.ToLower("JACK")).ToList();
  397. var t2 = db.Queryable<Student>().Where(it => SqlFunc.IsNull(it.Name,"nullvalue")=="nullvalue").ToList();
  398. var t3 = db.Queryable<Student>().Where(it => SqlFunc.MergeString("a",it.Name) == "nullvalue").ToList();
  399. var t3_sql = db.Queryable<Student>().Where(it => SqlFunc.MergeString("a", it.Name) == "nullvalue").ToSql();
  400. //SELECT [Id],[SchoolId],[Name],[CreateTime] FROM [Student] WHERE ((LOWER([Name])) = (LOWER(@MethodConst0)) )
  401. /***More Functions***/
  402. //SqlFunc.IsNullOrEmpty(object thisValue)
  403. //SqlFunc.ToLower(object thisValue)
  404. //SqlFunc.string ToUpper(object thisValue)
  405. //SqlFunc.string Trim(object thisValue)
  406. //SqlFunc.bool Contains(string thisValue, string parameterValue)
  407. //SqlFunc.ContainsArray(object[] thisValue, string parameterValue)
  408. //SqlFunc.StartsWith(object thisValue, string parameterValue)
  409. //SqlFunc.EndsWith(object thisValue, string parameterValue)
  410. //SqlFunc.Equals(object thisValue, object parameterValue)
  411. //SqlFunc.DateIsSame(DateTime date1, DateTime date2)
  412. //SqlFunc.DateIsSame(DateTime date1, DateTime date2, DateType dataType)
  413. //SqlFunc.DateAdd(DateTime date, int addValue, DateType millisecond)
  414. //SqlFunc.DateAdd(DateTime date, int addValue)
  415. //SqlFunc.DateValue(DateTime date, DateType dataType)
  416. //SqlFunc.Between(object value, object start, object end)
  417. //SqlFunc.ToInt32(object value)
  418. //SqlFunc.ToInt64(object value)
  419. //SqlFunc.ToDate(object value)
  420. //SqlFunc.ToString(object value)
  421. //SqlFunc.ToDecimal(object value)
  422. //SqlFunc.ToGuid(object value)
  423. //SqlFunc.ToDouble(object value)
  424. //SqlFunc.ToBool(object value)
  425. //SqlFunc.Substring(object value, int index, int length)
  426. //SqlFunc.Replace(object value, string oldChar, string newChar)
  427. //SqlFunc.Length(object value) { throw new NotImplementedException(); }
  428. //SqlFunc.AggregateSum(object thisValue)
  429. //SqlFunc.AggregateAvg<TResult>(TResult thisValue)
  430. //SqlFunc.AggregateMin(object thisValue)
  431. //SqlFunc.AggregateMax(object thisValue)
  432. //SqlFunc.AggregateCount(object thisValue)
  433. }
  434. public static void Select()
  435. {
  436. var db = GetInstance();
  437. db.IgnoreColumns.Add("TestId", "Student");
  438. var s1 = db.Queryable<Student>().Where(it => it.Id == 136915).Single();
  439. var s2 = db.Queryable<Student>().Select(it => new { id = it.Id, w = new { x = it } }).ToList();
  440. var s3 = db.Queryable<Student>().Select(it => new { newid = it.Id }).ToList();
  441. var s4 = db.Queryable<Student>().Select(it => new { newid = it.Id, obj = it }).ToList();
  442. var s41 = db.Queryable<Student>().Select<dynamic>("*").ToList();
  443. var s5 = db.Queryable<Student>().Select(it => new ViewModelStudent2 { Student = it, Name = it.Name }).ToList();
  444. var s6 = db.Queryable<Student, School>((st, sc) => new object[] {
  445. JoinType.Left,st.SchoolId==sc.Id
  446. })
  447. .OrderBy(st => st.Id, OrderByType.Desc)
  448. .OrderBy((st, sc) => sc.Id, OrderByType.Desc)
  449. .Select((st, sc) => new { st.Name, SchoolId = sc.Id }).ToList();
  450. var s7 = db.Queryable<Student, School>((st, sc) => new object[] {
  451. JoinType.Left,st.SchoolId==sc.Id
  452. }).Select((st, sc) => sc).ToList();
  453. var s8 = db.Queryable<Student, School>((st, sc) => new object[] {
  454. JoinType.Left,st.SchoolId==sc.Id
  455. })
  456. .OrderBy((st, sc) => st.SchoolId)
  457. .Select((st, sc) => sc)
  458. .Take(1).ToList();
  459. var s9 = db.Queryable<Student>().Select(it=>new Student() { Id=it.Id, TestId=1, Name=it.Name, CreateTime=it.CreateTime }).First();
  460. var s10 = db.Queryable<Student>().Select(it => new Student() { Id = it.Id}).First();
  461. }
  462. private static void Sqlable()
  463. {
  464. var db = GetInstance();
  465. var join3 = db.Queryable("Student", "st")
  466. .AddJoinInfo("School", "sh", "sh.id=st.schoolid")
  467. .Where("st.id>@id")
  468. .AddParameters(new { id = 1 })
  469. .Select("st.*").ToList();
  470. var join3_sql = db.Queryable("Student", "st")
  471. .AddJoinInfo("School", "sh", "sh.id=st.schoolid")
  472. .Where("st.id>@id")
  473. .AddParameters(new { id = 1 })
  474. .Select("st.*").ToSql();
  475. var join3_sql1 = db.Queryable<Student>()
  476. .AddJoinInfo("School", "sh", "sh.id=st.schoolid")
  477. .Where("st.id>@id")
  478. .AddParameters(new { id = 1,name=2 })
  479. .Select("st.*").ToSql();
  480. //SELECT st.* FROM [Student] st Left JOIN School sh ON sh.id=st.schoolid WHERE st.id>@id
  481. }
  482. private static void Enum()
  483. {
  484. var db = GetInstance();
  485. var list = db.Queryable<StudentEnum>().AS("Student").Where(it => it.SchoolId == SchoolEnum.HarvardUniversity).ToList();
  486. }
  487. }
  488. }