DotNetNext / SqlSugar

.Net aot ORM Fastest ORM Simple Easy VB.NET Sqlite orm Oracle ORM Mysql Orm 虚谷数据库 postgresql ORm SqlServer oRm 达梦 ORM 人大金仓 ORM 神通ORM C# ORM , C# ORM .NET ORM NET5 ORM .NET6 ORM ClickHouse orm QuestDb ,TDengine ORM,OceanBase orm,GaussDB orm ,Tidb orm Object/Relational Mapping
https://www.donet5.com/Home/Doc
MIT License
5.27k stars 1.33k forks source link

How to select where with " COLLATE SQL_Latin1_General_CP1_CI_AS " #1102

Closed mrreddevil2412 closed 1 year ago

mrreddevil2412 commented 1 year ago

Hello bro! I have query with keyword by Vietnamese. When I select not use " COLLATE SQL_Latin1_General_CP1_CI_AS " then => data =null

image USE COLLATE SQL_Latin1_General_CP1_CI_AS => data not null image

DotNetNext commented 1 year ago

Where("name COLLATE SQL_Latin1_General_CP1_CI_AS like @p",new { p="%"+keywork+"%"})

DotNetNext commented 1 year ago

Or extend sqlfunc method

   public class Demo
    {
        public static SqlSugarClient GetDb()
        {
            //Create ext method
            var expMethods = new List<SqlFuncExternal>();
            expMethods.Add(new SqlFuncExternal()
            {
                UniqueMethodName = "MyToString",
                MethodValue = (expInfo, dbType, expContext) =>
                {
                    if(dbType==DbType.SqlServer)
                     return string.Format("CAST({0} AS VARCHAR(MAX))", expInfo.Args[0].MemberName);
                    else
                     throw new Exception("未实现");
                }
            });

            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();

            //生成的Sql CAST([Id] AS VARCHAR(MAX))
        }
    }
mrreddevil2412 commented 1 year ago

Thanks! I used way where(string) easy 👍👍👍