whereIF怎么追加一个or条件? 返回
SqlSugar
沟通中
7
226
娇娇欲静 发布于1个月前
悬赏:0 飞吻
是从缓存中取出来的List类型的数据源,不能使用Expressionable.Create创建出来的条件
热忱回答(7)
-
娇娇欲静 VIP01个月前
暂时把两个条件放一个where中了,然后第二个判断条件用SqlFunc.IIF(SqlFunc.HasNumber(id), obj.Id == id, false),有点麻烦,请问有指定两个条件用什么来拼接的属性吗?
0 回复 -
fate sta VIP01个月前
用动态表达式
//用例1:连写 Expression<Func<Order, bool>> exp = Expressionable.Create<Order>() //创建表达式 .OrIF(p > 0, it => it.Id == p) .OrIF(name != null, it => it.Name == name && it.Sex==1) .ToExpression();//注意 这一句 不能少 var list = db.Queryable<Order>().Where(exp).ToList();//直接用就行了不需要判段 null和加true
0 回复 -
娇娇欲静 VIP01个月前
@fate sta:试过了,List<>类型的where不能用这种方法,会标红
var expUser = Expressionable.Create<Workerinfo>() .Or(obj => SqlFunc.ToLower(obj.Name).Contains(SqlFunc.ToLower(name))) .OrIF(SqlFunc.HasNumber(id), obj => obj.Id == id) .ToExpression(); list = cachedList //.Where(obj => SqlFunc.ToLower(obj.Name).Contains(SqlFunc.ToLower(name)) || SqlFunc.IIF(SqlFunc.HasNumber(id), obj.Id == id, false)) .Where(expUser);
完整代码是这样的,被注释掉的代码是我现在使用的代码,它不会报错,查询出来的数据页正常
0 回复 -
fate sta VIP01个月前
var list
0 回复 -
娇娇欲静 VIP01个月前
@fate sta:一样,数据源是List<>就会遇到这个问题
0 回复 -
fate sta VIP01个月前
还有问题提供DEMO不清楚你代码
0 回复 -
娇娇欲静 VIP01个月前
@fate sta:
ar key = "getWorkerList"; // 从缓存中获取数据 var cachedList = _memoryCache.Get(key) as List<Workerinfo>; if (cachedList == null) { // 数据不存在于缓存中,从数据库或其他来源获取数据,并缓存它 cachedList = SqlSugarBase.QYDB.Queryable<Workerinfo>().PartitionBy(it => it.Name).ToList(); var cacheEntryOptions = new MemoryCacheEntryOptions().SetSlidingExpiration(TimeSpan.FromSeconds(30)); // 添加数据到缓存中,并设置过期时间(可选) _memoryCache.Set(key, cachedList, cacheEntryOptions); } var list = cachedList.Where(obj => SqlFunc.ToLower(obj.Name).Contains(SqlFunc.ToLower(name)) || SqlFunc.IIF(SqlFunc.HasNumber(id), obj.Id == id, false)); //Expression<Func<Workerinfo, bool>> exp = Expressionable.Create<Workerinfo>() // .Or(obj => SqlFunc.ToLower(obj.Name).Contains(SqlFunc.ToLower(name))) // .OrIF(SqlFunc.HasNumber(id), obj => obj.Id == id) // .ToExpression(); //var list = cachedList.Where(exp); public class Workerinfo { // 对应数据库列:id,类型:int [SugarColumn(ColumnName = "id", ColumnDataType = "int", IsPrimaryKey = true, IsIdentity = true)] // 可根据需要设置主键、自增等属性 public int Id { get; set; } [SugarColumn(ColumnName = "name", ColumnDataType = "nvarchar", IsNullable = true)] public string? Name { get; set; } [SugarColumn(ColumnName = "sex", ColumnDataType = "nvarchar", IsNullable = true)] public string? Sex { get; set; } [SugarColumn(ColumnName = "Peoples", ColumnDataType = "nvarchar", IsNullable = true)] public string? Peoples { get; set; } [SugarColumn(ColumnName = "Mobile", ColumnDataType = "nvarchar", IsNullable = true)] public string? Mobile { get; set; } [SugarColumn(ColumnName = "OnJob", ColumnDataType = "nvarchar", IsNullable = true)] public string? OnJob { get; set; } }
0 回复