Home > Net >  C # lambda expressions can be like this?
C # lambda expressions can be like this?

Time:11-09

EF paging
 public IPage Page (int pageIndex, int pageSize, Expression Predicate, bool isAsc, 
Expression KeySelector) where TEntity: class
{
Using (BaseEntitiesContext context=new BaseEntitiesContext ())
{
If (pageIndex & lt;=0 & amp; & PageSize & lt;=0)
{
Throw new Exception (" pageIndex or pageSize cannot be less than or equal to zero!" );
}
IPage Page=new Page ()
{
PageIndex=PageIndex,
PageSize=PageSize
};
Int the skip=(pageIndex - 1) * pageSize;
If (predicate==null)
{
FutureCount fcount=context. Set FutureCount () ();
FutureQuery FutureQuery=isAsc
? The context. Set (a). OrderBy (keySelector.) Skip (Skip). Take (pageSize). The Future ()
: context. Set (). OrderByDescending (keySelector.) Skip (Skip). Take (pageSize). The Future ();
Page. TotalItems=fcount. Value;
Page. The Items=futureQuery. ToList ();
Page. TotalPages=page. TotalItems/pageSize;
If ((page. TotalItems % pageSize)!=0) page. TotalPages++;
}
The else
{
Var queryable=context. Set () Where (predicate);
FutureCount fcount=queryable. FutureCount ();
FutureQuery FutureQuery=isAsc
? Queryable. OrderBy (keySelector). Skip (Skip). Take (pageSize). The Future ()
: queryable OrderByDescending (keySelector.) Skip (Skip). Take (pageSize). The Future ();
Page. TotalItems=fcount. Value;
Page. The Items=futureQuery. ToList ();
Page. TotalPages=page. TotalItems/pageSize;
If ((page. TotalItems % pageSize)!=0) page. TotalPages++;
}
Return page;
}

}

Normal use
 using (BaseEntitiesContext context=new BaseEntitiesContext ()) 
{
Var result=context. Page}

In providing front-end, back-end parsed into Lamada
For example this is through the front end into the parameter
{
PageIndex: 1
PageSize: 50
FliterItems: [{fieldName: "UPDATER", compare: "EQ," value: "00000000-0000-0000-0000-000000000000"}]
SortFiled: DIC_SORT
SortOrder: isAsc
}
Parsing, now if the w.U PDATER =="00000000-0000-0000-0000-000000000000" can be written as w =="00000000-0000-0000-0000-000000000000" this kind of variable, the background is don't write too much if, else determine assembly lambda, could you under Daniel to achieve so?

CodePudding user response:

If the compiled without error, I think you can,

CodePudding user response:

We don't want to discuss this problem, we are more willing to say, don't go for EF,

Can look to the GraphQL, finish see GraphQL you will find that he is your entrance to this idea, although to also want to emit, also want to iqueryable, also want exspresstion, but our advice is to look at day, look at the road, don't always staring at the soles of the feet)


https://www.nuget.org/packages/GraphQL/3.0.0-preview-1648
https://www.nuget.org/packages/GraphQL.EntityFramework/11.0.0-beta.4

CodePudding user response:

I was writing,
 public virtual IQueryable FindPageList (int pageIndex, int pageSize, out int totalRecord, Expression WhereLamdba, bool isAsc Expression OrderLamdba) 
{
Var _list=dbEF. Set (a). AsNoTracking (). Where (whereLamdba);
TotalRecord=_list. The Count ();
If (isAsc) _list=_list. OrderBy (orderLamdba). Skip ((pageIndex - 1) * pageSize) Take (pageSize);
The else _list=_list. OrderByDescending (orderLamdba). Skip ((pageIndex - 1) * pageSize) Take (pageSize);
Return _list;
}

CodePudding user response:

I feel should be no problem
  •  Tags:  
  • LINQ
  • Related