mirror of
https://gitee.com/dotnetchina/SqlSugar.git
synced 2025-09-20 10:38:09 +08:00
-
This commit is contained in:
83
SqlServerTest/UnitTest/Query/JoinQuery.cs
Normal file
83
SqlServerTest/UnitTest/Query/JoinQuery.cs
Normal file
@@ -0,0 +1,83 @@
|
||||
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;
|
||||
namespace OrmTest.UnitTest
|
||||
{
|
||||
public class JoinQuery : UnitTestBase
|
||||
{
|
||||
private JoinQuery() { }
|
||||
public JoinQuery(int eachCount)
|
||||
{
|
||||
this.Count = eachCount;
|
||||
}
|
||||
internal void Init()
|
||||
{
|
||||
base.Begin();
|
||||
for (int i = 0; i < base.Count; i++)
|
||||
{
|
||||
Q1();
|
||||
Q2();
|
||||
Q3();
|
||||
}
|
||||
base.End("Method Test");
|
||||
}
|
||||
|
||||
private void Q3()
|
||||
{
|
||||
using (var db = GetInstance())
|
||||
{
|
||||
var join3 = db.Queryable("Student", "st")
|
||||
.AddJoinInfo("School", "sh", "sh.id=st.schoolid")
|
||||
.Where("st.id>@id")
|
||||
.AddParameters(new { id = 1 })
|
||||
.Select("st.*").ToSql();
|
||||
string sql = @"SELECT st.* FROM [STudent] st Left JOIN School sh ON sh.id=st.schoolid WHERE st.id>@id ";
|
||||
base.Check(sql,new List<SugarParameter>() {new SugarParameter("@id",1)}, join3.Key, join3.Value, "join 3 Error");
|
||||
}
|
||||
}
|
||||
|
||||
public void Q1()
|
||||
{
|
||||
using (var db = GetInstance())
|
||||
{
|
||||
var join1 = db.Queryable<Student, School>((st, sc) => new object[] {
|
||||
JoinType.Left,st.SchoolId==sc.Id
|
||||
}).Where(st => st.Id > 0).Select<Student>("*").ToSql();
|
||||
base.Check(@"SELECT * FROM [STudent] st Left JOIN School sc ON ( [st].[SchoolId] =[sc].[Id]) WHERE ( [st].[ID] > @Id0 ) ",
|
||||
new List<SugarParameter>() {
|
||||
new SugarParameter("@Id0",0)
|
||||
}, join1.Key, join1.Value, "join 1 Error");
|
||||
}
|
||||
}
|
||||
public void Q2()
|
||||
{
|
||||
using (var db = GetInstance())
|
||||
{
|
||||
var join2 = db.Queryable<Student, School>((st, sc) => new object[] {
|
||||
JoinType.Left,st.SchoolId==sc.Id
|
||||
}).Where(st => st.Id > 2).Select<Student>("*").ToSql();
|
||||
base.Check(@"SELECT * FROM [STudent] st Left JOIN School sc ON ( [st].[SchoolId] =[sc].[Id]) WHERE ( [st].[ID] > @Id0 ) ",
|
||||
new List<SugarParameter>() {
|
||||
new SugarParameter("@Id0",2)
|
||||
}, join2.Key, join2.Value, "join 1 Error");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public SqlSugarClient GetInstance()
|
||||
{
|
||||
SqlSugarClient db = new SqlSugarClient(new SystemTableConfig() { ConnectionString = Config.ConnectionString, DbType = DbType.SqlServer });
|
||||
db.Ado.IsEnableLogEvent = true;
|
||||
db.Ado.LogEventStarting = (sql, pars) =>
|
||||
{
|
||||
Console.WriteLine(sql + " " + pars);
|
||||
};
|
||||
return db;
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user