diff --git a/FreeSql.Tests/FreeSql.Tests/FreeSql.Tests.xml b/FreeSql.Tests/FreeSql.Tests/FreeSql.Tests.xml index c482a1d3f..bf9e6d4d6 100644 --- a/FreeSql.Tests/FreeSql.Tests/FreeSql.Tests.xml +++ b/FreeSql.Tests/FreeSql.Tests/FreeSql.Tests.xml @@ -24,6 +24,86 @@ 支付Id + + + 班期标识-BodyScheduleEntity班期主键 + + + + + 姓名 + + + + + 出生日期 + + + + + 身份证 + + + + + 联系电话 + + + + + 学校名称 + + + + + 初潮年龄 + + + + + 联系地址 + + + + + 审核人标识 + + + + + 审核人 + + + + + 审核时间 + + + + + 拒绝原因 + + + + + 采集时间 + + + + + 附件标识(多附件) + + + + + 性别 + + + + + 班期信息 + + 保存或添加,如果主键有值则尝试 Update,如果影响的行为 0 则尝试 Insert diff --git a/FreeSql.Tests/FreeSql.Tests/Issues/505.cs b/FreeSql.Tests/FreeSql.Tests/Issues/505.cs new file mode 100644 index 000000000..9a12b9ac3 --- /dev/null +++ b/FreeSql.Tests/FreeSql.Tests/Issues/505.cs @@ -0,0 +1,52 @@ +using FreeSql.DataAnnotations; +using Newtonsoft.Json; +using System; +using System.Collections.Generic; +using System.Diagnostics; +using System.Text; +using System.Threading; +using Xunit; + +namespace FreeSql.Tests.Issues +{ + public class _505 + { + [Fact] + public void ByteLengthTest() + { + TestLocal(g.sqlserver); + TestLocal(g.mysql); + TestLocal(g.pgsql); + TestLocal(g.oracle); + TestLocal(g.sqlite); + TestLocal(g.firebird); + TestLocal(g.dameng); + //TestLocal(g.kingbaseES); // + //TestLocal(g.shentong); // OCTET_LENGTH(xx) 返回结果32,值不符合 + //TestLocal(g.msaccess); //lenb(xx) 返回结果 15,值不符合 + + void TestLocal(IFreeSql fsql) + { + var byteArray = Encoding.UTF8.GetBytes("我是中国人"); + fsql.Delete().Where("1=1").ExecuteAffrows(); + fsql.Insert(new Model505 { ByteLength = byteArray.Length, ByteArray = byteArray }).ExecuteAffrows(); + + var item = fsql.Select() + //.Where(x => x.ByteArray.Length == x.ByteLength) + .First(a => new { item = a, length = a.ByteArray.Length }); + + Assert.NotNull(item); + Assert.Equal(Encoding.UTF8.GetString(byteArray), Encoding.UTF8.GetString(item.item.ByteArray)); + Assert.Equal(byteArray.Length, item.item.ByteLength); + Assert.Equal(byteArray.Length, item.length); + } + } + public class Model505 + { + public Guid id { get; set; } + public byte[] ByteArray { get; set; } + public int ByteLength { get; set; } + } + + } +} diff --git a/FreeSql.Tests/FreeSql.Tests/Issues/507.cs b/FreeSql.Tests/FreeSql.Tests/Issues/507.cs new file mode 100644 index 000000000..838844720 --- /dev/null +++ b/FreeSql.Tests/FreeSql.Tests/Issues/507.cs @@ -0,0 +1,163 @@ +using FreeSql.DataAnnotations; +using Newtonsoft.Json; +using System; +using System.Collections.Generic; +using System.Diagnostics; +using System.Text; +using System.Threading; +using Xunit; + +namespace FreeSql.Tests.Issues +{ + public class _507 + { + [Fact] + public void SelectTest() + { + var fsql = g.sqlite; + var _bodyAuditRepository = fsql.GetRepository(); + + var sql = _bodyAuditRepository.Select + .Include(a => a.ClassInfo) + .Count(out var total) + .OrderBy(true, a => a.ClassInfo.period) + .OrderBy(true, a => a.ClassInfo.className) + .Page(2, 10) + .ToList(); + } + public class BodyAuditEntity + { + public string Id { get; set; } + + /// + /// 班期标识-BodyScheduleEntity班期主键 + /// + [Column(Name = "ClassId", StringLength = 70, Position = 2)] + public string ClassId { get; set; } + + /// + /// 姓名 + /// + [Column(Name = "Name", StringLength = 20, Position = 3)] + public string Name { get; set; } + + + + /// + /// 出生日期 + /// + [Column(Name = "Birthday", StringLength = 20, Position = 5)] + public string Birthday { get; set; } + + /// + /// 身份证 + /// + [Column(Name = "IdCard", StringLength = 20, Position = 6)] + public string IdCard { get; set; } + + /// + /// 联系电话 + /// + [Column(Name = "TelPhone", StringLength = 15, Position = 7)] + public string TelPhone { get; set; } + + /// + /// 学校名称 + /// + [Column(Name = "SchoolName", StringLength = 50, Position = 8)] + public string SchoolName { get; set; } + + /// + /// 初潮年龄 + /// + [Column(Name = "StuCCNL", StringLength = 20, Position = 9)] + public int? StuCCNL { get; set; } + + /// + /// 联系地址 + /// + [Column(Name = "Address", StringLength = 100, Position = 10)] + public string Address { get; set; } + + /// + /// 审核人标识 + /// + [Column(Name = "CheckUserId", StringLength = 70)] + public string CheckUserId { get; set; } + + /// + /// 审核人 + /// + [Column(Name = "CheckUserName", StringLength = 20)] + public string CheckUserName { get; set; } + + /// + /// 审核时间 + /// + [Column(Name = "CheckTime")] + public DateTime? CheckTime { get; set; } + + /// + /// 拒绝原因 + /// + [Column(Name = "RefuseReason", StringLength = 100)] + public string RefuseReason { get; set; } + + /// + /// 采集时间 + /// + [Column(Name = "CollectionDate")] + public DateTime? CollectionDate { get; set; } + + /// + /// 附件标识(多附件) + /// + [Column(Name = "AttachmentId", StringLength = 500)] + public string AttachmentId { get; set; } + + /// + /// 性别 + /// + [Column(Name = "Sex", StringLength = 2, Position = 4)] + public string Sex { get; set; } + + #region 导航属性 + /// + /// 班期信息 + /// + [Navigate(nameof(ClassId))] + public virtual BodyScheduleEntity ClassInfo { get; set; } + #endregion + } + public class BodyScheduleEntity + { + public string Id { get; set; } + + [Column(Name = "period")] + public int? period { get; set; } + + [Column(Name = "maxQuota")] + public int? maxQuota { get; set; } + + [Column(Name = "allDays")] + public int? allDays { get; set; } + + [Column(Name = "schoolId", StringLength = 64)] + public string bodySchoolId { get; set; } + + [Column(Name = "ownership", StringLength = 64)] + public string ownership { get; set; } + + + [Column(Name = "className", StringLength = 20)] + public string className { get; set; } + + [Column(Name = "sex", StringLength = 10)] + public string sex { get; set; } + + [Column(Name = "classType", StringLength = 20)] + public string classType { get; set; } + } + + } +} diff --git a/Providers/FreeSql.Provider.Dameng/DamengExpression.cs b/Providers/FreeSql.Provider.Dameng/DamengExpression.cs index acc956e96..e97e9c88a 100644 --- a/Providers/FreeSql.Provider.Dameng/DamengExpression.cs +++ b/Providers/FreeSql.Provider.Dameng/DamengExpression.cs @@ -18,6 +18,10 @@ public override string ExpressionLambdaToSqlOther(Expression exp, ExpTSC tsc) Func getExp = exparg => ExpressionLambdaToSql(exparg, tsc); switch (exp.NodeType) { + case ExpressionType.ArrayLength: + var arrOper = (exp as UnaryExpression)?.Operand; + if (arrOper.Type == typeof(byte[])) return $"lengthb({getExp(arrOper)})"; + break; case ExpressionType.Convert: var operandExp = (exp as UnaryExpression)?.Operand; var gentype = exp.Type.NullableTypeOrThis(); diff --git a/Providers/FreeSql.Provider.Firebird/FirebirdExpression.cs b/Providers/FreeSql.Provider.Firebird/FirebirdExpression.cs index c2f6a1e13..f52cc8e68 100644 --- a/Providers/FreeSql.Provider.Firebird/FirebirdExpression.cs +++ b/Providers/FreeSql.Provider.Firebird/FirebirdExpression.cs @@ -18,6 +18,10 @@ public override string ExpressionLambdaToSqlOther(Expression exp, ExpTSC tsc) Func getExp = exparg => ExpressionLambdaToSql(exparg, tsc); switch (exp.NodeType) { + case ExpressionType.ArrayLength: + var arrOper = (exp as UnaryExpression)?.Operand; + if (arrOper.Type == typeof(byte[])) return $"octet_length({getExp(arrOper)})"; + break; case ExpressionType.Convert: var operandExp = (exp as UnaryExpression)?.Operand; var gentype = exp.Type.NullableTypeOrThis(); diff --git a/Providers/FreeSql.Provider.KingbaseES/KingbaseESExpression.cs b/Providers/FreeSql.Provider.KingbaseES/KingbaseESExpression.cs index 13f2b1953..1c174d31d 100644 --- a/Providers/FreeSql.Provider.KingbaseES/KingbaseESExpression.cs +++ b/Providers/FreeSql.Provider.KingbaseES/KingbaseESExpression.cs @@ -19,6 +19,12 @@ public override string ExpressionLambdaToSqlOther(Expression exp, ExpTSC tsc) Func getExp = exparg => ExpressionLambdaToSql(exparg, tsc); switch (exp.NodeType) { + case ExpressionType.ArrayLength: + var arrOper = (exp as UnaryExpression)?.Operand; + var arrOperExp = getExp(arrOper); + if (arrOperExp.StartsWith("(") || arrOperExp.EndsWith(")")) return $"array_length(array[{arrOperExp.TrimStart('(').TrimEnd(')')}],1)"; + if (arrOper.Type == typeof(byte[])) return $"octet_length({getExp(arrOper)})"; + return $"case when {arrOperExp} is null then 0 else array_length({arrOperExp},1) end"; case ExpressionType.Convert: var operandExp = (exp as UnaryExpression)?.Operand; var gentype = exp.Type.NullableTypeOrThis(); @@ -45,10 +51,6 @@ public override string ExpressionLambdaToSqlOther(Expression exp, ExpTSC tsc) } } break; - case ExpressionType.ArrayLength: - var arrOperExp = getExp((exp as UnaryExpression).Operand); - if (arrOperExp.StartsWith("(") || arrOperExp.EndsWith(")")) return $"array_length(array[{arrOperExp.TrimStart('(').TrimEnd(')')}],1)"; - return $"case when {arrOperExp} is null then 0 else array_length({arrOperExp},1) end"; case ExpressionType.Call: var callExp = exp as MethodCallExpression; diff --git a/Providers/FreeSql.Provider.MsAccess/MsAccessExpression.cs b/Providers/FreeSql.Provider.MsAccess/MsAccessExpression.cs index 0de463bce..337bba13e 100644 --- a/Providers/FreeSql.Provider.MsAccess/MsAccessExpression.cs +++ b/Providers/FreeSql.Provider.MsAccess/MsAccessExpression.cs @@ -19,6 +19,10 @@ public override string ExpressionLambdaToSqlOther(Expression exp, ExpTSC tsc) Func getExp = exparg => ExpressionLambdaToSql(exparg, tsc); switch (exp.NodeType) { + //case ExpressionType.ArrayLength: + // var arrOper = (exp as UnaryExpression)?.Operand; + // if (arrOper.Type == typeof(byte[])) return $"lenb({getExp(arrOper)})"; #505 + // break; case ExpressionType.Convert: var operandExp = (exp as UnaryExpression)?.Operand; var gentype = exp.Type.NullableTypeOrThis(); diff --git a/Providers/FreeSql.Provider.MySql/MySqlExpression.cs b/Providers/FreeSql.Provider.MySql/MySqlExpression.cs index d54733b2e..8f784c475 100644 --- a/Providers/FreeSql.Provider.MySql/MySqlExpression.cs +++ b/Providers/FreeSql.Provider.MySql/MySqlExpression.cs @@ -20,6 +20,10 @@ public override string ExpressionLambdaToSqlOther(Expression exp, ExpTSC tsc) Func getExp = exparg => ExpressionLambdaToSql(exparg, tsc); switch (exp.NodeType) { + case ExpressionType.ArrayLength: + var arrOper = (exp as UnaryExpression)?.Operand; + if (arrOper.Type == typeof(byte[])) return $"octet_length({getExp(arrOper)})"; + break; case ExpressionType.Convert: var operandExp = (exp as UnaryExpression)?.Operand; var gentype = exp.Type.NullableTypeOrThis(); diff --git a/Providers/FreeSql.Provider.Odbc/Dameng/OdbcDamengExpression.cs b/Providers/FreeSql.Provider.Odbc/Dameng/OdbcDamengExpression.cs index f7a344e30..0c9c5f846 100644 --- a/Providers/FreeSql.Provider.Odbc/Dameng/OdbcDamengExpression.cs +++ b/Providers/FreeSql.Provider.Odbc/Dameng/OdbcDamengExpression.cs @@ -18,6 +18,10 @@ public override string ExpressionLambdaToSqlOther(Expression exp, ExpTSC tsc) Func getExp = exparg => ExpressionLambdaToSql(exparg, tsc); switch (exp.NodeType) { + case ExpressionType.ArrayLength: + var arrOper = (exp as UnaryExpression)?.Operand; + if (arrOper.Type == typeof(byte[])) return $"lengthb({getExp(arrOper)})"; + break; case ExpressionType.Convert: var operandExp = (exp as UnaryExpression)?.Operand; var gentype = exp.Type.NullableTypeOrThis(); diff --git a/Providers/FreeSql.Provider.Odbc/KingbaseES/OdbcKingbaseESExpression.cs b/Providers/FreeSql.Provider.Odbc/KingbaseES/OdbcKingbaseESExpression.cs index f819d74e2..c9299d534 100644 --- a/Providers/FreeSql.Provider.Odbc/KingbaseES/OdbcKingbaseESExpression.cs +++ b/Providers/FreeSql.Provider.Odbc/KingbaseES/OdbcKingbaseESExpression.cs @@ -19,6 +19,12 @@ public override string ExpressionLambdaToSqlOther(Expression exp, ExpTSC tsc) Func getExp = exparg => ExpressionLambdaToSql(exparg, tsc); switch (exp.NodeType) { + case ExpressionType.ArrayLength: + var arrOper = (exp as UnaryExpression)?.Operand; + var arrOperExp = getExp(arrOper); + if (arrOperExp.StartsWith("(") || arrOperExp.EndsWith(")")) return $"array_length(array[{arrOperExp.TrimStart('(').TrimEnd(')')}],1)"; + if (arrOper.Type == typeof(byte[])) return $"octet_length({getExp(arrOper)})"; + return $"case when {arrOperExp} is null then 0 else array_length({arrOperExp},1) end"; case ExpressionType.Convert: var operandExp = (exp as UnaryExpression)?.Operand; var gentype = exp.Type.NullableTypeOrThis(); @@ -45,10 +51,6 @@ public override string ExpressionLambdaToSqlOther(Expression exp, ExpTSC tsc) } } break; - case ExpressionType.ArrayLength: - var arrOperExp = getExp((exp as UnaryExpression).Operand); - if (arrOperExp.StartsWith("(") || arrOperExp.EndsWith(")")) return $"array_length(array[{arrOperExp.TrimStart('(').TrimEnd(')')}],1)"; - return $"case when {arrOperExp} is null then 0 else array_length({arrOperExp},1) end"; case ExpressionType.Call: var callExp = exp as MethodCallExpression; diff --git a/Providers/FreeSql.Provider.Odbc/MySql/OdbcMySqlExpression.cs b/Providers/FreeSql.Provider.Odbc/MySql/OdbcMySqlExpression.cs index 0867c0788..bbf238d41 100644 --- a/Providers/FreeSql.Provider.Odbc/MySql/OdbcMySqlExpression.cs +++ b/Providers/FreeSql.Provider.Odbc/MySql/OdbcMySqlExpression.cs @@ -18,6 +18,10 @@ public override string ExpressionLambdaToSqlOther(Expression exp, ExpTSC tsc) Func getExp = exparg => ExpressionLambdaToSql(exparg, tsc); switch (exp.NodeType) { + case ExpressionType.ArrayLength: + var arrOper = (exp as UnaryExpression)?.Operand; + if (arrOper.Type == typeof(byte[])) return $"octet_length({getExp(arrOper)})"; + break; case ExpressionType.Convert: var operandExp = (exp as UnaryExpression)?.Operand; var gentype = exp.Type.NullableTypeOrThis(); diff --git a/Providers/FreeSql.Provider.Odbc/Oracle/OdbcOracleExpression.cs b/Providers/FreeSql.Provider.Odbc/Oracle/OdbcOracleExpression.cs index 7ee2af4ab..f1adc6503 100644 --- a/Providers/FreeSql.Provider.Odbc/Oracle/OdbcOracleExpression.cs +++ b/Providers/FreeSql.Provider.Odbc/Oracle/OdbcOracleExpression.cs @@ -18,6 +18,10 @@ public override string ExpressionLambdaToSqlOther(Expression exp, ExpTSC tsc) Func getExp = exparg => ExpressionLambdaToSql(exparg, tsc); switch (exp.NodeType) { + case ExpressionType.ArrayLength: + var arrOper = (exp as UnaryExpression)?.Operand; + if (arrOper.Type == typeof(byte[])) return $"dbms_lob.getlength({getExp(arrOper)})"; + break; case ExpressionType.Convert: var operandExp = (exp as UnaryExpression)?.Operand; var gentype = exp.Type.NullableTypeOrThis(); diff --git a/Providers/FreeSql.Provider.Odbc/PostgreSQL/OdbcPostgreSQLExpression.cs b/Providers/FreeSql.Provider.Odbc/PostgreSQL/OdbcPostgreSQLExpression.cs index d4fc852d7..fb9324799 100644 --- a/Providers/FreeSql.Provider.Odbc/PostgreSQL/OdbcPostgreSQLExpression.cs +++ b/Providers/FreeSql.Provider.Odbc/PostgreSQL/OdbcPostgreSQLExpression.cs @@ -19,6 +19,12 @@ public override string ExpressionLambdaToSqlOther(Expression exp, ExpTSC tsc) Func getExp = exparg => ExpressionLambdaToSql(exparg, tsc); switch (exp.NodeType) { + case ExpressionType.ArrayLength: + var arrOper = (exp as UnaryExpression)?.Operand; + var arrOperExp = getExp(arrOper); + if (arrOperExp.StartsWith("(") || arrOperExp.EndsWith(")")) return $"array_length(array[{arrOperExp.TrimStart('(').TrimEnd(')')}],1)"; + if (arrOper.Type == typeof(byte[])) return $"octet_length({getExp(arrOper)})"; + return $"case when {arrOperExp} is null then 0 else array_length({arrOperExp},1) end"; case ExpressionType.Convert: var operandExp = (exp as UnaryExpression)?.Operand; var gentype = exp.Type.NullableTypeOrThis(); @@ -45,10 +51,6 @@ public override string ExpressionLambdaToSqlOther(Expression exp, ExpTSC tsc) } } break; - case ExpressionType.ArrayLength: - var arrOperExp = getExp((exp as UnaryExpression).Operand); - if (arrOperExp.StartsWith("(") || arrOperExp.EndsWith(")")) return $"array_length(array[{arrOperExp.TrimStart('(').TrimEnd(')')}],1)"; - return $"case when {arrOperExp} is null then 0 else array_length({arrOperExp},1) end"; case ExpressionType.Call: var callExp = exp as MethodCallExpression; diff --git a/Providers/FreeSql.Provider.Odbc/SqlServer/OdbcSqlServerExpression.cs b/Providers/FreeSql.Provider.Odbc/SqlServer/OdbcSqlServerExpression.cs index 65e0779d0..977032376 100644 --- a/Providers/FreeSql.Provider.Odbc/SqlServer/OdbcSqlServerExpression.cs +++ b/Providers/FreeSql.Provider.Odbc/SqlServer/OdbcSqlServerExpression.cs @@ -18,6 +18,10 @@ public override string ExpressionLambdaToSqlOther(Expression exp, ExpTSC tsc) Func getExp = exparg => ExpressionLambdaToSql(exparg, tsc); switch (exp.NodeType) { + case ExpressionType.ArrayLength: + var arrOper = (exp as UnaryExpression)?.Operand; + if (arrOper.Type == typeof(byte[])) return $"datalength({getExp(arrOper)})"; + break; case ExpressionType.Convert: var operandExp = (exp as UnaryExpression)?.Operand; var gentype = exp.Type.NullableTypeOrThis(); diff --git a/Providers/FreeSql.Provider.Oracle/OracleExpression.cs b/Providers/FreeSql.Provider.Oracle/OracleExpression.cs index 0d483ca35..d29a40419 100644 --- a/Providers/FreeSql.Provider.Oracle/OracleExpression.cs +++ b/Providers/FreeSql.Provider.Oracle/OracleExpression.cs @@ -18,6 +18,10 @@ public override string ExpressionLambdaToSqlOther(Expression exp, ExpTSC tsc) Func getExp = exparg => ExpressionLambdaToSql(exparg, tsc); switch (exp.NodeType) { + case ExpressionType.ArrayLength: + var arrOper = (exp as UnaryExpression)?.Operand; + if (arrOper.Type == typeof(byte[])) return $"dbms_lob.getlength({getExp(arrOper)})"; + break; case ExpressionType.Convert: var operandExp = (exp as UnaryExpression)?.Operand; var gentype = exp.Type.NullableTypeOrThis(); diff --git a/Providers/FreeSql.Provider.PostgreSQL/PostgreSQLExpression.cs b/Providers/FreeSql.Provider.PostgreSQL/PostgreSQLExpression.cs index f584472ca..d2524cb41 100644 --- a/Providers/FreeSql.Provider.PostgreSQL/PostgreSQLExpression.cs +++ b/Providers/FreeSql.Provider.PostgreSQL/PostgreSQLExpression.cs @@ -20,6 +20,12 @@ public override string ExpressionLambdaToSqlOther(Expression exp, ExpTSC tsc) Func getExp = exparg => ExpressionLambdaToSql(exparg, tsc); switch (exp.NodeType) { + case ExpressionType.ArrayLength: + var arrOper = (exp as UnaryExpression)?.Operand; + var arrOperExp = getExp(arrOper); + if (arrOperExp.StartsWith("(") || arrOperExp.EndsWith(")")) return $"array_length(array[{arrOperExp.TrimStart('(').TrimEnd(')')}],1)"; + if (arrOper.Type == typeof(byte[])) return $"octet_length({getExp(arrOper)})"; + return $"case when {arrOperExp} is null then 0 else array_length({arrOperExp},1) end"; case ExpressionType.Convert: var operandExp = (exp as UnaryExpression)?.Operand; var gentype = exp.Type.NullableTypeOrThis(); @@ -46,10 +52,6 @@ public override string ExpressionLambdaToSqlOther(Expression exp, ExpTSC tsc) } } break; - case ExpressionType.ArrayLength: - var arrOperExp = getExp((exp as UnaryExpression).Operand); - if (arrOperExp.StartsWith("(") || arrOperExp.EndsWith(")")) return $"array_length(array[{arrOperExp.TrimStart('(').TrimEnd(')')}],1)"; - return $"case when {arrOperExp} is null then 0 else array_length({arrOperExp},1) end"; case ExpressionType.Call: var callExp = exp as MethodCallExpression; diff --git a/Providers/FreeSql.Provider.ShenTong/ShenTongExpression.cs b/Providers/FreeSql.Provider.ShenTong/ShenTongExpression.cs index aedaa4ebd..81fc85924 100644 --- a/Providers/FreeSql.Provider.ShenTong/ShenTongExpression.cs +++ b/Providers/FreeSql.Provider.ShenTong/ShenTongExpression.cs @@ -19,6 +19,12 @@ public override string ExpressionLambdaToSqlOther(Expression exp, ExpTSC tsc) Func getExp = exparg => ExpressionLambdaToSql(exparg, tsc); switch (exp.NodeType) { + case ExpressionType.ArrayLength: + var arrOper = (exp as UnaryExpression)?.Operand; + var arrOperExp = getExp(arrOper); + if (arrOperExp.StartsWith("(") || arrOperExp.EndsWith(")")) return $"array_length(array[{arrOperExp.TrimStart('(').TrimEnd(')')}],1)"; + //if (arrOper.Type == typeof(byte[])) return $"octet_length({getExp(arrOper)})"; #505 + return $"case when {arrOperExp} is null then 0 else array_length({arrOperExp},1) end"; case ExpressionType.Convert: var operandExp = (exp as UnaryExpression)?.Operand; var gentype = exp.Type.NullableTypeOrThis(); @@ -45,10 +51,6 @@ public override string ExpressionLambdaToSqlOther(Expression exp, ExpTSC tsc) } } break; - case ExpressionType.ArrayLength: - var arrOperExp = getExp((exp as UnaryExpression).Operand); - if (arrOperExp.StartsWith("(") || arrOperExp.EndsWith(")")) return $"array_length(array[{arrOperExp.TrimStart('(').TrimEnd(')')}],1)"; - return $"case when {arrOperExp} is null then 0 else array_length({arrOperExp},1) end"; case ExpressionType.Call: var callExp = exp as MethodCallExpression; diff --git a/Providers/FreeSql.Provider.SqlServer/SqlServerExpression.cs b/Providers/FreeSql.Provider.SqlServer/SqlServerExpression.cs index 91ead6ed6..ce8ddd091 100644 --- a/Providers/FreeSql.Provider.SqlServer/SqlServerExpression.cs +++ b/Providers/FreeSql.Provider.SqlServer/SqlServerExpression.cs @@ -18,6 +18,10 @@ public override string ExpressionLambdaToSqlOther(Expression exp, ExpTSC tsc) Func getExp = exparg => ExpressionLambdaToSql(exparg, tsc); switch (exp.NodeType) { + case ExpressionType.ArrayLength: + var arrOper = (exp as UnaryExpression)?.Operand; + if (arrOper.Type == typeof(byte[])) return $"datalength({getExp(arrOper)})"; + break; case ExpressionType.Convert: var operandExp = (exp as UnaryExpression)?.Operand; var gentype = exp.Type.NullableTypeOrThis(); diff --git a/Providers/FreeSql.Provider.Sqlite/SqliteExpression.cs b/Providers/FreeSql.Provider.Sqlite/SqliteExpression.cs index fa3c394a8..b6413643c 100644 --- a/Providers/FreeSql.Provider.Sqlite/SqliteExpression.cs +++ b/Providers/FreeSql.Provider.Sqlite/SqliteExpression.cs @@ -18,6 +18,10 @@ public override string ExpressionLambdaToSqlOther(Expression exp, ExpTSC tsc) Func getExp = exparg => ExpressionLambdaToSql(exparg, tsc); switch (exp.NodeType) { + case ExpressionType.ArrayLength: + var arrOper = (exp as UnaryExpression)?.Operand; + if (arrOper.Type == typeof(byte[])) return $"length({getExp(arrOper)})"; + break; case ExpressionType.Convert: var operandExp = (exp as UnaryExpression)?.Operand; var gentype = exp.Type.NullableTypeOrThis();