Subquery WhereIF bug

This commit is contained in:
sunkaixuan 2019-01-11 21:11:47 +08:00
parent 5753377cdf
commit 5f9b18ab5f
2 changed files with 67 additions and 10 deletions

View File

@ -9,9 +9,10 @@ namespace OrmTest.BugTest
{
public class Bug2
{
public SqlSugarClient DB
public SqlSugarClient DB
{
get {
get
{
SqlSugarClient db = new SqlSugarClient(new ConnectionConfig()
{
InitKeyType = InitKeyType.Attribute,
@ -22,15 +23,71 @@ namespace OrmTest.BugTest
return db;
}
}
public void Init() {
var x2= DB.Queryable<School>().Where(x => x.Id == SqlFunc.Subqueryable<School>().Where(y => y.Id == SqlFunc.Subqueryable<Student>().Where(yy => y.Id == x.Id).Select(yy => yy.Id)).Select(y => y.Id)).ToSql();
if (!x2.Key.Contains("STudent")) {
// throw new Exception("bug2 error");
public void Init()
{
var x2 = DB.Queryable<School>().Where(x => x.Id == SqlFunc.Subqueryable<School>().Where(y => y.Id == SqlFunc.Subqueryable<Student>().Where(yy => y.Id == x.Id).Select(yy => yy.Id)).Select(y => y.Id)).ToSql();
if (!x2.Key.Contains("STudent"))
{
// throw new Exception("bug2 error");
}
var UserNameOrName = "111";
var OrganizationUnitId = 0;
var RoleId = 0;
var sql = DB.Queryable<User>().//一对多的子查询
WhereIF(!string.IsNullOrWhiteSpace(UserNameOrName), t1 => t1.Name.Contains(UserNameOrName)).
Where(t1 =>
SqlFunc.Subqueryable<UserOrganizationUnit>().
Where(t2 => t2.UserId == t1.Id).
WhereIF(OrganizationUnitId > 0, t2 => t2.OrganizationUnitId == OrganizationUnitId).Any())
// Where(t1 => SqlFunc.Subqueryable<UserRole>().
//Where(t3 => t3.UserId == t1.Id).
//WhereIF(RoleId > 0, t3 => t3.RoleId == RoleId).Any())
.Select(t1 => new User { Id = SqlFunc.GetSelfAndAutoFill(t1.Id) }).ToSql();
}
}
///<summary>
///用户信息表
///</summary>
public partial class User
{
///<summary>
/// 描述:主键
/// 默认值:
/// 是否可空: False
///</summary>
[SugarColumn(IsPrimaryKey = true, IsIdentity = true)]
public long Id { get; set; }
public string Name { get; set; }
}
public partial class UserOrganizationUnit
{
[SugarColumn(IsPrimaryKey = true, IsIdentity = true)]
public long Id { get; set; }
public long UserId { get; set; }
public long OrganizationUnitId { get; set; }
}
///<summary>
///用户角色关系表
///</summary>
public partial class UserRole
{
[SugarColumn(IsPrimaryKey = true, IsIdentity = true)]
public long Id { get; set; }
public long UserId { get; set; }
public int RoleId { get; set; }
}
}
}

View File

@ -50,7 +50,7 @@ namespace SqlSugar
}
var isWhere= Convert.ToBoolean(value);
if (!Convert.ToBoolean(isWhere)) {
return "";
return "WHERE 1=1 ";
}
var argExp = exp.Arguments[1];
var result = "WHERE " + SubTools.GetMethodValue(Context, argExp, ResolveExpressType.WhereMultiple); ;