LINQ Expression 多条件复合条件组合(And/Or)
LINQ Expression 多条件复合条件组合(And/Or)
需要组合以下复合条件
SQL 全选
DELETE Table Where UserId='admin' AND (exp1 or exp2)
Linq
C# 全选
//删除资料. SQL:Where UserId='admin' AND (exp1 or exp2)
Expression<Func<tb_Group, bool>> exp1 = w => w.UserId == _Loginer.Account;//UserId条件
Expression<Func<tb_Group, bool>> expOr1 = w => false;//初始Or条件
if (delSystemStockPool) expOr1 = expOr1.Or(w => listSystem.Contains(w.GroupName));//系统组
if (delCustomStockPool) expOr1 = expOr1.Or(w => listCustom.Contains(w.GroupName));//用户自定义组
var y = _Database.Remove(exp1.And(expOr1));//IDatabase实例,删除记录
LinqExtensions
C# 全选
public static partial class LinqExtensions
{
/// <summary>
/// 自定义Expression的And扩展方法
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="exp1"></param>
/// <param name="exp2"></param>
/// <returns></returns>
public static Expression<Func<T, bool>> And<T>(this Expression<Func<T, bool>> exp1,
Expression<Func<T, bool>> exp2)
{
var invokedExpr = Expression.Invoke(exp2, exp1.Parameters.Cast<Expression>());
return Expression.Lambda<Func<T, bool>>(Expression.AndAlso(exp1.Body, invokedExpr), exp1.Parameters);
}
/// <summary>
/// 自定义Expression的Or扩展方法
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="exp1"></param>
/// <param name="exp2"></param>
/// <returns></returns>
public static Expression<Func<T, bool>> Or<T>(this Expression<Func<T, bool>> exp1,
Expression<Func<T, bool>> exp2)
{
var invokedExpr = Expression.Invoke(exp2, exp1.Parameters.Cast<Expression>());
return Expression.Lambda<Func<T, bool>>(Expression.OrElse(exp1.Body, invokedExpr), exp1.Parameters);
}
}
CSFramework.EF 数据库框架
IDatabase实例
https://www.cscode.net/archive/csframework.ef/363596745297925.html
版权声明:本文为开发框架文库发布内容,转载请附上原文出处连接
NewDoc C/S框架网