Home > Net >  How to call Func <ticket, bool> where parameters, dynamic splicing query
How to call Func <ticket, bool> where parameters, dynamic splicing query

Time:11-26

Have a paging method is as follows:
 
///& lt; summary>
///paging method
///& lt;/summary>
///& lt; Param name="where" & gt; Conditions & lt;/param>
///& lt; Param name="order" & gt; Sorting & lt;/param>
///& lt; Param name="pageIndex & gt;" The current page & lt;/param>
///& lt; Param name="pageSize & gt;" Each page data volume & lt;/param>
///& lt; Param name="count" & gt; The total number of & lt;/param>
///& lt; Returns>
[NonAction]
Private List GetPageData (Func Where, Func The order, int pageIndex, int pageSize, out int the count)
{
Using (phoneTicketEntity db=new phoneTicketEntity ())
{
Var queryList=the where!=null? Db. Tickets. AsNoTracking (). The Where (Where) : db. Tickets. AsNoTracking ();

QueryList=the order!=null? QueryList. OrderByDescending (order) : queryList;

Count=queryList. The count ();

Return queryList. Skip ((pageIndex - 1) * pageSize.) Take (pageSize). ToList ();
}
}
# endregion


//the front-end calls getJsonData () returns json data, this method calls the function of the paging, parameters are need a dynamic query field,
//which applied getPageData method requires the where parameters, is clearly wrong, can only query to satisfy a condition, how to modify, please give directions

Public ActionResult getJsonData (int page=1, int limit=10, string username="", string fpcode=" ", string fpnumber=", "
String price_min=", "string price_max=" ", string fpdate="")
{
int count=0;

Func Where=t=& gt; T.d eptid==((user) Session [] "user"). The ID;//only to check the current department

if (! String. IsNullOrEmpty (username))
Where=t=& gt; T.u sername==username;

if (! String. IsNullOrEmpty (fpcode))
Where=t=& gt; T.f pcode==fpcode;

if (! String. IsNullOrEmpty (fpnumber))
Where=t=& gt; T.f pnumber==fpnumber;

If (! String. IsNullOrEmpty (price_min))
Where=t=& gt; T.f pmoney & gt;=the Convert. ToDecimal (price_min);

if (! String. IsNullOrEmpty (price_max))
Where=t=& gt; T.f pmoney & lt;=the Convert. ToDecimal (price_max);

if (! String. IsNullOrEmpty (fpdate))
Where=t=& gt; T.f pdate. ToString ()==fpdate;


//if (! String. IsNullOrEmpty (fpdatestart) & amp; & ! String. IsNullOrEmpty (fpdateend))
//the where=t=& gt; Within the scope of t.f pdate. AddDays//date


Var tickets=getPageData (where, t=& gt; T.f pdate, page limit, out count);

Return a Json (
New JsonFormatResult
{
The Count=Count,
Code=Code. Ok,
Msg="Msg. Ok,"
Data=https://bbs.csdn.net/topics/tickets
}, JsonRequestBehavior AllowGet
);
}
  •  Tags:  
  • C#
  • Related