mirror of
https://gitee.com/dotnetchina/SqlSugar.git
synced 2025-10-26 18:59:17 +08:00
Support custom SqlFunc Method
This commit is contained in:
53
Src/Asp.Net/SqlServerTest/Demos/C_ExtSqlFun.cs
Normal file
53
Src/Asp.Net/SqlServerTest/Demos/C_ExtSqlFun.cs
Normal file
@@ -0,0 +1,53 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using SqlSugar;
|
||||
using OrmTest.Models;
|
||||
|
||||
namespace OrmTest.Demo
|
||||
{
|
||||
public class ExtSqlFun : DemoBase
|
||||
{
|
||||
public static SqlSugarClient GetDb()
|
||||
{
|
||||
//Create ext method
|
||||
var expMethods = new List<SqlFuncExternal>();
|
||||
expMethods.Add(new SqlFuncExternal()
|
||||
{
|
||||
UniqueMethodName = "MyToString",
|
||||
MethodValue = (expInfo, dbType, expContext) =>
|
||||
{
|
||||
return string.Format("CAST({0} AS VARCHAR(MAX))", expInfo.Args[0].MemberName);
|
||||
}
|
||||
});
|
||||
|
||||
var config = new ConnectionConfig()
|
||||
{
|
||||
ConnectionString = Config.ConnectionString,
|
||||
DbType = DbType.SqlServer,
|
||||
IsAutoCloseConnection = true,
|
||||
ConfigureExternalServices = new ConfigureExternalServices()
|
||||
{
|
||||
SqlFuncServices = expMethods//set ext method
|
||||
}
|
||||
};
|
||||
|
||||
SqlSugarClient db = new SqlSugarClient(config);
|
||||
return db;
|
||||
}
|
||||
|
||||
public static string MyToString<T>(T str)
|
||||
{
|
||||
throw new NotSupportedException("Can only be used in expressions");
|
||||
}
|
||||
|
||||
public static void Init()
|
||||
{
|
||||
var db = GetDb();
|
||||
var list = db.Queryable<Student>().Where(it => MyToString(it.Id) == "1302583").ToList();
|
||||
var sql = db.Queryable<Student>().Where(it => MyToString(it.Id) == "1302583").ToSql();
|
||||
Console.WriteLine(sql);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -48,6 +48,7 @@ namespace OrmTest
|
||||
OrmTest.Demo.Aop.Init();
|
||||
OrmTest.Demo.MasterSlave.Init();
|
||||
OrmTest.Demo.SharedConnection.Init();
|
||||
OrmTest.Demo.ExtSqlFun.Init();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -56,6 +56,7 @@
|
||||
<Compile Include="Demos\4_Delete.cs" />
|
||||
<Compile Include="Demos\9_Aop.cs" />
|
||||
<Compile Include="Demos\B_SharedConnection.cs" />
|
||||
<Compile Include="Demos\C_ExtSqlFun.cs" />
|
||||
<Compile Include="Demos\DemoBase.cs" />
|
||||
<Compile Include="Demos\3_Insert.cs" />
|
||||
<Compile Include="Demos\1_Query.cs" />
|
||||
|
||||
Reference in New Issue
Block a user