Lambda Expression append condition judgment Expression & lt; Func & lt; T, bool & gt;, funcbool

Source: Internet
Author: User

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

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.