sqlsugar 扩展In 包含查询
一、添加扩展
①、配置扩展方法
# 扩展 In 包含查询: var context = new SqlSugarScope(new ConnectionConfig() { DbType = DbType.MySqlConnector, InitKeyType = InitKeyType.Attribute, IsAutoCloseConnection = true, ConnectionString = RepositoryConfig.ConnectionString, ConfigureExternalServices = new ConfigureExternalServices() { SqlFuncServices = new List<SqlFuncExternal> { new SqlFuncExternal() { UniqueMethodName="In", MethodValue=(methodExp, dbType, context)=> { List<string> list=new List<string>(); var @params=context?.Parameters[0]?.Value as IEnumerable<dynamic>; foreach (var key in @params ?? new object[]{}) { if (key.GetType()?.IsValueType ?? false) list.Add($"{key}"); else if(key.GetType()==typeof(string)) list.Add($"'{key}'"); } if(!list.Any()) return $" {methodExp.Args[1].MemberName} is not null "; return $" {methodExp.Args[1].MemberName} in ({string.Join(',',list)}) "; } } } } });
②、使用扩展方法
# 查询包含数据 var users = new List<string> { "apricot", "1764564459" }; # 使用: var user = context.Queryable<User>(p => SqlFuncEx.In(users, p.Name)).FirstAsync();
③、扩展类
# 方法扩展 public static class SqlFuncEx { public static bool In<T>(List<T> sources, T compare) => throw new NotSupportedException("Can only be used in expressions"); }
二、参考
①、 更多参考
如有帮助,欢迎转载,转载请注明原文链接:https://www.cnblogs.com/study10000/p/17049621.html

浙公网安备 33010602011771号