mirror of
https://gitee.com/dotnetchina/SqlSugar.git
synced 2025-10-15 18:55:07 +08:00
Subquery
This commit is contained in:
@@ -33,9 +33,12 @@ namespace OrmTest.Demo
|
||||
private static void Subqueryable()
|
||||
{
|
||||
var db = GetInstance();
|
||||
var getAll1 = db.Queryable<Student>()
|
||||
.Where(it => it.Id == SqlFunc.Subqueryable<School>().Where(s => s.Id == it.Id).Select(s => s.Id))
|
||||
.ToList();
|
||||
|
||||
var getAll7 = db.Queryable<Student>().Where(it => SqlFunc.Subqueryable<School>().Where(s => s.Id == it.Id).Any()).ToList();
|
||||
|
||||
var getAll8= db.Queryable<Student>().Where(it => SqlFunc.Subqueryable<School>().Where(s => s.Id == it.Id).NotAny()).ToList();
|
||||
|
||||
var getAll1 = db.Queryable<Student>().Where(it => it.Id == SqlFunc.Subqueryable<School>().Where(s => s.Id == it.Id).Select(s => s.Id)).ToList();
|
||||
|
||||
var getAll2 = db.Queryable<Student, School>((st, sc) => new object[] {
|
||||
JoinType.Left,st.Id==sc.Id
|
||||
@@ -46,24 +49,28 @@ namespace OrmTest.Demo
|
||||
var getAll3 = db.Queryable<Student, School>((st, sc) => new object[] {
|
||||
JoinType.Left,st.Id==sc.Id
|
||||
})
|
||||
.Select(st=>
|
||||
new {
|
||||
name=st.Name,
|
||||
id = SqlFunc.Subqueryable<School>().Where(s => s.Id == st.Id).Select(s => s.Id)
|
||||
.Select(st =>
|
||||
new
|
||||
{
|
||||
name = st.Name,
|
||||
id = SqlFunc.Subqueryable<School>().Where(s => s.Id == st.Id).Select(s => s.Id)
|
||||
})
|
||||
.ToList();
|
||||
|
||||
var getAll4 = db.Queryable<Student>().Select(it =>
|
||||
new {
|
||||
name = it.Name,
|
||||
id = SqlFunc.Subqueryable<School>().Where(s => s.Id == it.Id).Select(s => s.Id)
|
||||
}).ToList();
|
||||
new
|
||||
{
|
||||
name = it.Name,
|
||||
id = SqlFunc.Subqueryable<School>().Where(s => s.Id == it.Id).Select(s => s.Id)
|
||||
}).ToList();
|
||||
|
||||
var getAll5 = db.Queryable<Student>().Select(it =>
|
||||
new Student {
|
||||
new Student
|
||||
{
|
||||
Name = it.Name,
|
||||
Id = SqlFunc.Subqueryable<School>().Where(s => s.Id == it.Id).Select(s => s.Id)
|
||||
}).ToList();
|
||||
|
||||
}
|
||||
|
||||
private static void Async()
|
||||
|
@@ -25,13 +25,13 @@ namespace SqlSugar
|
||||
{
|
||||
get
|
||||
{
|
||||
return 1000;
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
public string GetValue(ExpressionContext context, Expression expression)
|
||||
{
|
||||
return ">0";
|
||||
return "EXISTS";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -0,0 +1,37 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Linq.Expressions;
|
||||
using System.Text;
|
||||
|
||||
namespace SqlSugar
|
||||
{
|
||||
public class SubLeftBracket : ISubOperation
|
||||
{
|
||||
public Expression Expression
|
||||
{
|
||||
get;set;
|
||||
}
|
||||
|
||||
public string Name
|
||||
{
|
||||
get
|
||||
{
|
||||
return "LeftBracket";
|
||||
}
|
||||
}
|
||||
|
||||
public int Sort
|
||||
{
|
||||
get
|
||||
{
|
||||
return 50;
|
||||
}
|
||||
}
|
||||
|
||||
public string GetValue(ExpressionContext context, Expression expression)
|
||||
{
|
||||
return "(";
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,37 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Linq.Expressions;
|
||||
using System.Text;
|
||||
|
||||
namespace SqlSugar
|
||||
{
|
||||
public class SubNotAny : ISubOperation
|
||||
{
|
||||
public Expression Expression
|
||||
{
|
||||
get; set;
|
||||
}
|
||||
|
||||
public string Name
|
||||
{
|
||||
get
|
||||
{
|
||||
return "NotAny";
|
||||
}
|
||||
}
|
||||
|
||||
public int Sort
|
||||
{
|
||||
get
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
public string GetValue(ExpressionContext context, Expression expression)
|
||||
{
|
||||
return "NOT EXISTS";
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,37 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Linq.Expressions;
|
||||
using System.Text;
|
||||
|
||||
namespace SqlSugar
|
||||
{
|
||||
public class SubRightBracket : ISubOperation
|
||||
{
|
||||
public Expression Expression
|
||||
{
|
||||
get;set;
|
||||
}
|
||||
|
||||
public string Name
|
||||
{
|
||||
get
|
||||
{
|
||||
return "RightBracket";
|
||||
}
|
||||
}
|
||||
|
||||
public int Sort
|
||||
{
|
||||
get
|
||||
{
|
||||
return 500;
|
||||
}
|
||||
}
|
||||
|
||||
public string GetValue(ExpressionContext context, Expression expression)
|
||||
{
|
||||
return ")";
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,36 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Linq.Expressions;
|
||||
using System.Text;
|
||||
|
||||
namespace SqlSugar
|
||||
{
|
||||
public class SubSelectDefault : ISubOperation
|
||||
{
|
||||
public Expression Expression
|
||||
{
|
||||
get;set;
|
||||
}
|
||||
|
||||
public string Name
|
||||
{
|
||||
get {
|
||||
return "SelectDefault";
|
||||
}
|
||||
}
|
||||
|
||||
public int Sort
|
||||
{
|
||||
get
|
||||
{
|
||||
return 250;
|
||||
}
|
||||
}
|
||||
|
||||
public string GetValue(ExpressionContext context, Expression expression)
|
||||
{
|
||||
return "*";
|
||||
}
|
||||
}
|
||||
}
|
@@ -47,9 +47,14 @@ namespace SqlSugar
|
||||
var item = SubTools.SubItems.First(s => s.Name == methodName);
|
||||
item.Expression = exp;
|
||||
return item;
|
||||
})
|
||||
.OrderBy(it => it.Sort).ToList();
|
||||
}).ToList();
|
||||
isubList.Insert(0, new SubBegin());
|
||||
if (isubList.Any(it => it is SubAny||it is SubNotAny)) {
|
||||
isubList.Add(new SubLeftBracket());
|
||||
isubList.Add(new SubRightBracket());
|
||||
isubList.Add(new SubSelectDefault());
|
||||
}
|
||||
isubList= isubList.OrderBy(it => it.Sort).ToList();
|
||||
List<string> result = isubList.Select(it =>
|
||||
{
|
||||
return it.GetValue(this.context, it.Expression);
|
||||
|
@@ -14,6 +14,7 @@ namespace SqlSugar
|
||||
new SubSelect(),
|
||||
new SubWhere(),
|
||||
new SubAny(),
|
||||
new SubNotAny(),
|
||||
new SubBegin(),
|
||||
new SubFromTable()
|
||||
};
|
||||
|
@@ -31,6 +31,11 @@ namespace SqlSugar
|
||||
return default(bool);
|
||||
}
|
||||
|
||||
public bool NotAny()
|
||||
{
|
||||
return default(bool);
|
||||
}
|
||||
|
||||
public bool Count()
|
||||
{
|
||||
return default(bool);
|
||||
|
@@ -73,10 +73,14 @@
|
||||
<Compile Include="Abstract\DeleteProvider\DeleteableProvider.cs" />
|
||||
<Compile Include="Enum\DbType.cs" />
|
||||
<Compile Include="ExpressionsToSql\Subquery\Items\ISubAction.cs" />
|
||||
<Compile Include="ExpressionsToSql\Subquery\Items\SubLeftBracket.cs" />
|
||||
<Compile Include="ExpressionsToSql\Subquery\Items\SubFromTable.cs" />
|
||||
<Compile Include="ExpressionsToSql\Subquery\Items\SubAny.cs" />
|
||||
<Compile Include="ExpressionsToSql\Subquery\Items\SubBegin.cs" />
|
||||
<Compile Include="ExpressionsToSql\Subquery\Items\SubRightBracket.cs" />
|
||||
<Compile Include="ExpressionsToSql\Subquery\Items\SubSelect.cs" />
|
||||
<Compile Include="ExpressionsToSql\Subquery\Items\SubSelectDefault.cs" />
|
||||
<Compile Include="ExpressionsToSql\Subquery\Items\SubNotAny.cs" />
|
||||
<Compile Include="ExpressionsToSql\Subquery\Subquerable.cs" />
|
||||
<Compile Include="ExpressionsToSql\Subquery\Items\SubWhere.cs" />
|
||||
<Compile Include="ExpressionsToSql\Subquery\SubResolve.cs" />
|
||||
|
Reference in New Issue
Block a user