Lambda Expression append condition judgment Expression <Func <T, bool>, funcbool
Public static class PredicateBuilder
{
/// <Summary>
/// When the organ function is applied to True: one AND is valid, AND multiple AND are valid; one OR is invalid, AND multiple OR is invalid; OR is valid after AND when mixing.
/// </Summary>
/// <Typeparam name = "T"> </typeparam>
/// <Returns> </returns>
Public static Expression <Func <T, bool> True <T> () {return f => true ;}
/// <Summary>
/// When the function is set to False: A single AND is invalid, AND multiple AND are invalid; a single OR is valid, AND multiple OR are valid; AND is valid after OR when mixing.
/// </Summary>
/// <Typeparam name = "T"> </typeparam>
/// <Returns> </returns>
Public static Expression <Func <T, bool> False <T> () {return f => false ;}
Public static Expression <Func <T, bool> Or <T> (this Expression <Func <T, bool> expr1,
Expression <Func <T, bool> expr2)
{
Var invokedExpr = Expression. Invoke (expr2, expr1.Parameters. Cast <Expression> ());
Return Expression. Lambda <Func <T, bool>
(Expression. Or (expr1.Body, invokedExpr), expr1.Parameters );
}
Public static Expression <Func <T, bool> And <T> (this Expression <Func <T, bool> expr1,
Expression <Func <T, bool> expr2)
{
Var invokedExpr = Expression. Invoke (expr2, expr1.Parameters. Cast <Expression> ());
Return Expression. Lambda <Func <T, bool>
(Expression. And (expr1.Body, invokedExpr), expr1.Parameters );
}
}
Bytes ---------------------------------------------------------------------------------------
Var where = PredicateBuilder. True <SYS_Message> ();
Where = where. And (p => p. isActive = true );
If (receiveUserId! = Null)
{
Where = where. And (p => p. MessageReceiveUser = receiveUserId );
}
If (! String. IsNullOrEmpty (messageMain ))
{
Where = where. And (p => messageMain. Contains (p. MessageMain ));
}
If (! String. IsNullOrEmpty (datagrusername ))
{
Where = where. and (p => messageMain. contains (p. messageMain) & aliexpressEntities. YH_User.Where (U => U. isActive = true & amp; policrusername. contains (U. first_name )). select (U => U. userId ). toList (). contains (Convert. toInt32 (p. messageReceiveUser )));
}
If (startTime! = Null)
{
Where = where. And (p => p. createTime> = startTime );
}
If (endTime! = Null)
{
Where = where. And (p => p. createTime <= endTime );
}
EntityList = aliexpressEntities. SYS_Message.Where (where. compile ()). orderByDescending (p => p. createTime ). skip (int) pageIndex-1) * pageSize ). take (pageSize ). toList ();
ProtectCount = aliexpressEntities. SYS_Message.Where (where. Compile (). Count ();
ProtectPage = Convert. ToInt32 (Math. Ceiling (Convert. ToDouble (protectCount)/pageSize ));