Home > Net >  Lambda expressions in c # dynamic field
Lambda expressions in c # dynamic field

Time:10-10

Front a drop-down box, have, time and ID two sorting options, and then I choose any options, ordered by the field name of any options
For example List The userList=userList. OrderBy (a=& gt; Anderson, d). ToList ();
Which can dynamically modify field, Anderson d ordered by the field name of the

List The userList=userList. OrderBy (a=& gt; A. (dynamic parameters). ToList ();

CodePudding user response:

With expression trees or reflection,
Reflect a simpler,
List The userList=userList. OrderBy (a=& gt; Al-qeada etType (.) GetProperty (" field name "). The GetValue (a, null). The ToString ()). ToList ();

CodePudding user response:

According to the parameters sorted with just switch
With reflective do more harm than good, and if it is EF? How do you counter

CodePudding user response:

Useless!

reference 1st floor caozhy response:
with expression trees or reflection,
Reflect a simpler,
List The userList=userList. OrderBy (a=& gt; Al-qeada etType (.) GetProperty (" field name "). The GetValue (a, null). The ToString ()). ToList ();

CodePudding user response:

I think, can achieve a sort method, then the userList. MySort

CodePudding user response:

The dynamic.

CodePudding user response:

OrderBy (a=& gt; A. (dynamic parameters)
- "custom a fun, return to sort the column type,

CodePudding user response:

You made some time ago did this
String orderExpression=the string. Format (" {0} {1} ", sortField, sortType==sortType. Asc? "Asc" : "desc");
Query=query. OrderBy (orderExpression);
SortField is to sort the field name, sortType is defined by the enumeration, to judge the positive and negative sequence
  •  Tags:  
  • C#
  • Related