mirror of
https://gitee.com/dotnetchina/SqlSugar.git
synced 2025-08-23 22:11:36 +08:00
Add Subqueryable.Where(sql)
This commit is contained in:
parent
3b4fc3e1d5
commit
4eec972e48
@ -17,6 +17,22 @@ namespace OrmTest
|
||||
NoEntity();
|
||||
Mapper();
|
||||
SqlFuncTest();
|
||||
Subquery();
|
||||
}
|
||||
|
||||
private static void Subquery()
|
||||
{
|
||||
Console.WriteLine("");
|
||||
Console.WriteLine("#### Subquery Start ####");
|
||||
var db = GetInstance();
|
||||
|
||||
var list = db.Queryable<Order>().Take(10).Select(it => new
|
||||
{
|
||||
customName=SqlFunc.Subqueryable<Custom>().Where("it.CustomId=id").Select(s=>s.Name),
|
||||
customName2 = SqlFunc.Subqueryable<Custom>().Where("it.CustomId = id").Where(s => true).Select(s => s.Name)
|
||||
}).ToList();
|
||||
|
||||
Console.WriteLine("#### Subquery End ####");
|
||||
}
|
||||
|
||||
private static void SqlFuncTest()
|
||||
@ -26,7 +42,7 @@ namespace OrmTest
|
||||
var db = GetInstance();
|
||||
var index= db.Queryable<Order>().Select(it => SqlFunc.CharIndex("a", "cccacc")).First();
|
||||
|
||||
Console.WriteLine("#### End Start ####");
|
||||
Console.WriteLine("#### SqlFunc End ####");
|
||||
}
|
||||
|
||||
private static void Mapper()
|
||||
|
@ -3,6 +3,7 @@ using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Linq.Expressions;
|
||||
using System.Text;
|
||||
using System.Text.RegularExpressions;
|
||||
|
||||
namespace SqlSugar
|
||||
{
|
||||
@ -41,6 +42,15 @@ namespace SqlSugar
|
||||
var exp = expression as MethodCallExpression;
|
||||
var argExp = exp.Arguments[0];
|
||||
var result = "AND " + SubTools.GetMethodValue(this.Context, argExp, ResolveExpressType.WhereMultiple);
|
||||
|
||||
|
||||
var regex = @"^AND (\@Const\d+) $";
|
||||
if (Regex.IsMatch(result, regex))
|
||||
{
|
||||
result = "AND " + this.Context.Parameters.First(it => it.ParameterName == Regex.Match(result, regex).Groups[1].Value).Value;
|
||||
return result;
|
||||
}
|
||||
|
||||
var selfParameterName = this.Context.GetTranslationColumnName((argExp as LambdaExpression).Parameters.First().Name) + UtilConstants.Dot;
|
||||
result = result.Replace(selfParameterName, SubTools.GetSubReplace(this.Context));
|
||||
return result;
|
||||
|
@ -3,6 +3,7 @@ using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Linq.Expressions;
|
||||
using System.Text;
|
||||
using System.Text.RegularExpressions;
|
||||
|
||||
namespace SqlSugar
|
||||
{
|
||||
@ -40,7 +41,16 @@ namespace SqlSugar
|
||||
{
|
||||
var exp = expression as MethodCallExpression;
|
||||
var argExp= exp.Arguments[0];
|
||||
var result= "WHERE "+SubTools.GetMethodValue(Context, argExp, ResolveExpressType.WhereMultiple);;
|
||||
var result= "WHERE "+SubTools.GetMethodValue(Context, argExp, ResolveExpressType.WhereMultiple);
|
||||
|
||||
|
||||
var regex = @"^WHERE (\@Const\d+) $";
|
||||
if (Regex.IsMatch(result, regex))
|
||||
{
|
||||
result = "WHERE " + this.Context.Parameters.First(it => it.ParameterName == Regex.Match(result, regex).Groups[1].Value).Value;
|
||||
return result;
|
||||
}
|
||||
|
||||
var selfParameterName = Context.GetTranslationColumnName((argExp as LambdaExpression).Parameters.First().Name)+UtilConstants.Dot;
|
||||
result = result.Replace(selfParameterName,SubTools.GetSubReplace(this.Context));
|
||||
return result;
|
||||
|
@ -9,6 +9,10 @@ namespace SqlSugar
|
||||
|
||||
public class Subqueryable<T> where T : class, new()
|
||||
{
|
||||
public Subqueryable<T> Where(string where)
|
||||
{
|
||||
return this;
|
||||
}
|
||||
public Subqueryable<T> Where(Func<T, bool> expression)
|
||||
{
|
||||
return this;
|
||||
|
Loading…
Reference in New Issue
Block a user