修复嵌套表达式

This commit is contained in:
xuejiaming 2023-11-20 10:50:32 +08:00
parent 8d4066433c
commit a34db8c165
2 changed files with 36 additions and 3 deletions

View File

@ -447,6 +447,8 @@ namespace Sample.MySql.Controllers
[HttpGet]
public async Task<IActionResult> Get16()
{
_defaultTableDbContext.ReadWriteSeparationWriteOnly();
var sysUserMod = await _defaultTableDbContext.Set<SysUserMod>().FirstOrDefaultAsync();
sysUserMod.Age = new Random().Next(1,999);
await _defaultTableDbContext.SaveChangesAsync();
@ -483,6 +485,18 @@ namespace Sample.MySql.Controllers
// var sysUserMods2 = await _defaultTableDbContext.Set<SysTest>().FromSqlRaw("select * from SysTest where id='2'").ToListAsync();
return Ok(sysUserMods);
}
[HttpGet]
public async Task<IActionResult> Get20()
{
var dateTime = new DateTime(2022,1,1);
var queryable = _defaultTableDbContext.Set<SysUserLogByMonth>().Where(o=>o.Time>dateTime).Select(o=>o.Id);
var sysUserMods = await _defaultTableDbContext.SysUserMod.AsNoTracking()
.Where(o => queryable.Contains(o.Id)).ToListAsync();
// var sysUserMods1 = await _defaultTableDbContext.Set<SysUserMod>().FromSqlRaw("select * from SysUserMod where id='2'").ToListAsync();
// var sysUserMods2 = await _defaultTableDbContext.Set<SysTest>().FromSqlRaw("select * from SysTest where id='2'").ToListAsync();
return Ok(sysUserMods);
}
// public void batachSave()
// {

View File

@ -25,7 +25,6 @@ namespace ShardingCore.Sharding.Parsers.Visitors
/// Email: 326308290@qq.com
internal class ShardingQueryPrepareVisitor : ExpressionVisitor
{
private readonly IShardingDbContext _shardingDbContext;
private bool isNotSupport;
private ShardingQueryableUseConnectionModeOptions shardingQueryableUseConnectionModeOptions;
private ShardingQueryableAsRouteOptions shardingQueryableAsRouteOptions;
@ -41,10 +40,13 @@ namespace ShardingCore.Sharding.Parsers.Visitors
public ShardingQueryPrepareVisitor(IShardingDbContext shardingDbContext)
{
_shardingDbContext = shardingDbContext;
_trackerManager = ((DbContext)shardingDbContext).GetShardingRuntimeContext()
.GetTrackerManager();
}
public ShardingQueryPrepareVisitor(ITrackerManager trackerManager)
{
_trackerManager = trackerManager;
}
public ShardingPrepareResult GetShardingPrepareResult()
{
return new ShardingPrepareResult(isNotSupport,
@ -108,7 +110,13 @@ namespace ShardingCore.Sharding.Parsers.Visitors
object value = fieldInfo.GetValue(container);
if (value is IQueryable queryable)
{
TryAddShardingEntities(queryable.ElementType, queryable);
var shardingQueryPrepareVisitor = new ShardingQueryPrepareVisitor(_trackerManager);
shardingQueryPrepareVisitor.Visit(queryable.Expression);
var shardingPrepareResult = shardingQueryPrepareVisitor.GetShardingPrepareResult();
foreach (var keyValuePair in shardingPrepareResult.QueryEntities)
{
TryAddShardingEntities(keyValuePair.Key, keyValuePair.Value??queryable);
}
}
//return Expression.Constant(value);
}
@ -194,6 +202,17 @@ namespace ShardingCore.Sharding.Parsers.Visitors
.Last();
return node.Arguments[0];
}
else
{
for (var i = 0; i < node.Arguments.Count; i++)
{
var nodeArgument = node.Arguments[i];
if (typeof(IQueryable).IsAssignableFrom(nodeArgument.Type))
{
Visit(nodeArgument);
}
}
}
}
return null;