SqlSugar/OrmTest/UnitTest/Query/SelectQuery.cs

74 lines
2.5 KiB
C#
Raw Normal View History

2017-03-04 01:22:06 +08:00
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SqlSugar;
using System.Linq.Expressions;
using OrmTest.Models;
2017-03-04 15:07:58 +08:00
namespace OrmTest.UnitTest
2017-03-04 01:22:06 +08:00
{
2017-03-05 00:29:53 +08:00
public class SelectQuery : ExpTestBase
2017-03-04 01:22:06 +08:00
{
2017-03-05 00:29:53 +08:00
private SelectQuery() { }
public SelectQuery(int eachCount)
2017-03-04 01:22:06 +08:00
{
this.Count = eachCount;
}
internal void Init()
{
base.Begin();
for (int i = 0; i < base.Count; i++)
{
Q2();
}
base.End("Method Test");
}
public void Q2()
{
using (var db = GetInstance())
{
2017-03-11 09:51:58 +08:00
db.Database.IsEnableLogEvent = true;
2017-03-16 10:42:31 +08:00
db.Database.LogEventStarting = (sql, pars) =>
2017-03-11 09:51:58 +08:00
{
2017-03-16 10:42:31 +08:00
Console.WriteLine(sql + " " + pars);
2017-03-11 09:51:58 +08:00
};
2017-03-16 10:42:31 +08:00
2017-03-26 15:18:17 +08:00
var listx = db.Queryable<School, School>((st, st2) => new object[] {
JoinType.Left,st.Id==st2.Id
})
.Where(st => st.Id > 0)
.Select<School, School, dynamic>((st, st2) => new {stid = st.Id, scId = st2.Id,xx=st }).ToList();
return;
2017-03-16 10:42:31 +08:00
var list = db.Queryable<School, School>((st, st2) => new object[] {
2017-03-13 13:51:02 +08:00
JoinType.Left,st.Id==st2.Id
2017-03-16 10:42:31 +08:00
}).Where<Student, School>((st, sc) => st.Id > 0)
.Select(st => new ViewModelStudent { School = st }).ToList();
2017-03-08 14:01:41 +08:00
2017-03-11 10:28:33 +08:00
var list2 = db.Queryable<Student>()
.Where(st => st.Id > 0)
.Select("id").ToList();
2017-03-08 16:46:34 +08:00
2017-03-16 10:42:31 +08:00
var list3 = db.Queryable<Student, School, School>((st, sc, sc2) => new object[] {
2017-03-08 16:46:34 +08:00
JoinType.Left,st.SchoolId==sc.Id,
JoinType.Left,sc2.Id==sc.Id
2017-03-11 10:16:05 +08:00
}).Where(st => st.Id > 0)
2017-03-16 10:42:31 +08:00
.Select<School>((st) => new School() { Id = st.Id }).ToList();
2017-03-08 14:01:41 +08:00
2017-03-11 10:28:33 +08:00
var list4 = db.Queryable("Student", "st")
2017-03-08 14:51:52 +08:00
.AddJoinInfo("School", "sh", "sh.id=st.schoolid")
.Where("st.id>@id")
2017-03-16 10:42:31 +08:00
.AddParameters(new { id = 1 })
2017-03-08 14:51:52 +08:00
.Select("st.*").ToList();
2017-03-04 01:22:06 +08:00
}
}
public SqlSugarClient GetInstance()
{
SqlSugarClient db = new SqlSugarClient(new SystemTablesConfig() { ConnectionString = Config.ConnectionString, DbType = DbType.SqlServer });
return db;
}
}
}