sqlsugar动态查询

This commit is contained in:
yubaolee 2023-08-27 00:23:45 +08:00
parent a6c79bea7c
commit 38fee56701
2 changed files with 19 additions and 6 deletions

View File

@ -181,6 +181,17 @@ namespace Infrastructure
return query;
}
public static ISugarQueryable<T> GenerateFilter<T>(this ISugarQueryable<T> query, string parametername, string filterjson)
{
if (!string.IsNullOrEmpty(filterjson))
{
var filterGroup = JsonHelper.Instance.Deserialize<FilterGroup>(filterjson);
query = GenerateFilter(query, parametername, filterGroup);
}
return query;
}
/// <summary>
/// 转换FilterGroup为Lambda表达式

View File

@ -3,6 +3,8 @@ using System.Linq;
using Infrastructure;
using NUnit.Framework;
using Microsoft.Extensions.DependencyInjection;
using OpenAuth.Repository.Domain;
using SqlSugar;
namespace OpenAuth.Repository.Test
{
@ -11,7 +13,7 @@ namespace OpenAuth.Repository.Test
[Test]
public void GenerateFilter()
{
var dbcontext = _autofacServiceProvider.GetService<OpenAuthDBContext>();
var sugarClient = _autofacServiceProvider.GetService<ISqlSugarClient>();
var json = @"
{
""Operation"": ""and"",
@ -50,8 +52,8 @@ namespace OpenAuth.Repository.Test
}
";
var query = dbcontext.Users.GenerateFilter("c",json);
Console.WriteLine(query.Expression.ToString());
var query = sugarClient.Queryable<User>().GenerateFilter("c",json);
Console.WriteLine(query.ToSqlString());
Console.WriteLine(JsonHelper.Instance.Serialize(query.ToList()));
}
@ -84,10 +86,10 @@ namespace OpenAuth.Repository.Test
sub
};
var dbcontext = _autofacServiceProvider.GetService<OpenAuthDBContext>();
var sugarClient = _autofacServiceProvider.GetService<ISqlSugarClient>();
var query = dbcontext.Users.GenerateFilter("c",JsonHelper.Instance.Serialize(filterGroup));
Console.WriteLine(query.Expression.ToString());
var query = sugarClient.Queryable<User>().GenerateFilter("c",filterGroup);
Console.WriteLine(query.ToSqlString());
}
}
}