Update mysql sum

This commit is contained in:
sunkaixuna 2021-12-24 11:48:11 +08:00
parent 0eb6be3cfc
commit 829e25ea4d
4 changed files with 120 additions and 1 deletions

View File

@ -86,6 +86,7 @@
<Compile Include="Models\ViewOrder.cs" /> <Compile Include="Models\ViewOrder.cs" />
<Compile Include="Demo\DemoJ_Report.cs" /> <Compile Include="Demo\DemoJ_Report.cs" />
<Compile Include="UnitTest\UInsert.cs" /> <Compile Include="UnitTest\UInsert.cs" />
<Compile Include="UnitTest\UnitCustom01.cs" />
<Compile Include="UnitTest\UQueue.cs" /> <Compile Include="UnitTest\UQueue.cs" />
<Compile Include="Program.cs" /> <Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" /> <Compile Include="Properties\AssemblyInfo.cs" />

View File

@ -31,6 +31,7 @@ namespace OrmTest
} }
public static void Init() public static void Init()
{ {
UnitCustom01.Init();
Insert(); Insert();
Queue(); Queue();
CodeFirst(); CodeFirst();

View File

@ -0,0 +1,117 @@
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace OrmTest
{
public class UnitCustom01
{
public static void Init()
{
var ssc = new SqlSugarClient(new ConnectionConfig()
{
ConnectionString = OrmTest.Config.ConnectionString,
DbType = SqlSugar.DbType.MySql, //必填
IsAutoCloseConnection = true
});
ssc.CodeFirst.InitTables<Student>();
var expMethods = new List<SqlFuncExternal>();
expMethods.Add(new SqlFuncExternal()
{
UniqueMethodName = "SumSugar",
MethodValue = (expInfo, dbType, expContext) =>
{
if (dbType == DbType.SqlServer)
return string.Format("SUM({0})", expInfo.Args[0].MemberName);
else if (dbType == DbType.MySql)
return string.Format("SUM({0})", expInfo.Args[0].MemberName);
else
throw new Exception("未实现");
}
});
ssc.CurrentConnectionConfig.ConfigureExternalServices = new ConfigureExternalServices()
{
SqlFuncServices = expMethods //set ext method
};
try
{
ssc.Insertable(new Student() { Age = 1, Createtime = DateTime.Now, Grade = 1, Id = 1, Name = "a", Schoolid = 1 }).ExecuteCommand();
ssc.Insertable(new Student() { Age = 1, Createtime = DateTime.Now, Grade = 1, Id = 1, Name = "a", Schoolid = 1 }).ExecuteCommand();
var sss12 = ssc.Queryable<Student>().GroupBy(o => o.Name).Select(o => new { Age = SqlFunc.AggregateSum(o.Age) }).ToList();
}
catch (Exception e)
{
}
}
}
[SugarTable("unitstudent1111")]
public class Student
{
/// <summary>
///
///</summary>
[SugarColumn(ColumnName = "id", IsPrimaryKey = true, IsIdentity = true)]
public int Id { get; set; }
/// <summary>
///
///</summary>
[SugarColumn(ColumnName = "schoolid")]
public int? Schoolid { get; set; }
/// <summary>
///
///</summary>
[SugarColumn(ColumnName = "name")]
public string Name { get; set; }
/// <summary>
///
///</summary>
[SugarColumn(ColumnName = "createtime")]
public DateTime? Createtime { get; set; }
/// <summary>
///
///</summary>
[SugarColumn(ColumnName = "age")]
public int? Age { get; set; }
/// <summary>
///
///</summary>
[SugarColumn(ColumnName = "grade")]
public int? Grade { get; set; }
}
}

View File

@ -336,7 +336,7 @@ namespace SqlSugar
addValue = null; addValue = null;
} }
} }
else if (item.PropertyType == UtilConstants.IntType) else if (UtilMethods.GetUnderType(item.PropertyType) == UtilConstants.IntType)
{ {
addValue = Convert.ToInt32(addValue); addValue = Convert.ToInt32(addValue);
} }