Browse Source

one to many support any(y=>y.id>0)

pull/2/head
skx 4 years ago
parent
commit
059ca5650e
  1. 5
      Src/Asp.Net/SqlServerTest/UnitTest/UQueryable2.cs
  2. 41
      Src/Asp.Net/SqlSugar/ExpressionsToSql/ResolveItems/MapperExpressionResolve.cs

5
Src/Asp.Net/SqlServerTest/UnitTest/UQueryable2.cs

@ -32,7 +32,10 @@ namespace OrmTest
.Mapper(it => it.Items, it => it.Items.First().OrderId)
.Where(it => it.Items.Any())
.ToList();
var list7= Db.Queryable<Order>()
.Mapper(it => it.Items, it => it.Items.First().OrderId)
.Where(it => it.Items.Any(y => y.ItemId == 1))
.ToList();
var sql=Db.Queryable<Order>().AS("[order]").ToList();

41
Src/Asp.Net/SqlSugar/ExpressionsToSql/ResolveItems/MapperExpressionResolve.cs

@ -45,7 +45,7 @@ namespace SqlSugar
//var SelectInfo = GetSelectInfo(expression);
this.context.InitMappingInfo(childExpression.Expression.Type);
var entity = this.context.EntityMaintenance.GetEntityInfo(childExpression.Expression.Type);
oneToMany(callName, entity, childExpression.Expression.ToString(), fillInfo, mappingFild1Info, mappingFild1Info2);
oneToMany(methodExpression, callName, entity, childExpression.Expression.ToString(), fillInfo, mappingFild1Info, mappingFild1Info2);
}
private void ResolveMember()
@ -99,33 +99,60 @@ namespace SqlSugar
.Select(sqlBuilder.GetTranslationColumnName(selectInfo.FieldName)).ToSql().Key;
}
private void oneToMany(string methodName,EntityInfo mainEntity,string shortName,MapperExpressionInfo fillInfo, MapperExpressionInfo mappingFild1Info, MapperExpressionInfo mappingFild1Info2)
private void oneToMany(MethodCallExpression methodCallExpression,string methodName,EntityInfo mainEntity,string shortName,MapperExpressionInfo fillInfo, MapperExpressionInfo mappingFild1Info, MapperExpressionInfo mappingFild1Info2)
{
var pkColumn = mainEntity.Columns.FirstOrDefault(it=>it.IsPrimarykey==true);
var pkColumn = mainEntity.Columns.FirstOrDefault(it => it.IsPrimarykey == true);
if (pkColumn == null)
{
pkColumn = mainEntity.Columns.FirstOrDefault();
}
var tableName = sqlBuilder.GetTranslationTableName(fillInfo.EntityInfo.DbTableName);
var whereLeft = sqlBuilder.GetTranslationColumnName(mappingFild1Info.FieldString);
var whereRight = sqlBuilder.GetTranslationColumnName(shortName+"."+pkColumn.DbColumnName);
var whereRight = sqlBuilder.GetTranslationColumnName(shortName + "." + pkColumn.DbColumnName);
string whereExpression = GetWhereExpression(methodCallExpression);
if (methodName == "Any")
{
this.sql = " ("+this.context.Queryable<object>()
this.sql = " (" + this.context.Queryable<object>()
.AS(tableName)
.Where(string.Format(" {0}={1} ", whereLeft, whereRight))
.Select("COUNT(1)").ToSql().Key+")>0 ";
.WhereIF(!string.IsNullOrEmpty(whereExpression), whereExpression)
.Select("COUNT(1)").ToSql().Key + ")>0 ";
}
else
{
this.sql = this.context.Queryable<object>()
.AS(tableName)
.Where(string.Format(" {0}={1} ", whereLeft, whereRight))
.WhereIF(!string.IsNullOrEmpty(whereExpression), whereExpression)
.Select("COUNT(1)").ToSql().Key;
}
}
private string GetWhereExpression(MethodCallExpression methodCallExpression)
{
if (methodCallExpression.Arguments.Count <= 1)
return null;
var exp= methodCallExpression.Arguments[1];
var querybuiler=InstanceFactory.GetQueryBuilder(this.context.CurrentConnectionConfig);
querybuiler.LambdaExpressions = InstanceFactory.GetLambdaExpressions(this.context.CurrentConnectionConfig);
querybuiler.Builder = InstanceFactory.GetSqlbuilder(this.context.CurrentConnectionConfig);
querybuiler.Builder.Context = querybuiler.Context;
querybuiler.Builder.QueryBuilder = querybuiler;
querybuiler.Context = this.context;
var expValue=querybuiler.GetExpressionValue(exp, ResolveExpressType.WhereMultiple);
var paramterName = (exp as LambdaExpression).Parameters[0].Name;
var sql = expValue.GetResultString();
sql = sql.Replace(querybuiler.Builder.GetTranslationColumnName(paramterName) + ".", "");
if (querybuiler.Parameters != null && querybuiler.Parameters.Count > 0)
{
foreach (var item in querybuiler.Parameters)
{
sql = sql.Replace(item.ParameterName,item.Value.ObjToString().ToSqlValue());
}
}
return sql;
}
private MapperExpressionInfo GetSelectInfo(Expression expression)
{

Loading…
Cancel
Save