Build: no, this is not built in linq expression, but the c # compiler at compile time (note that is a compile time, rather than a runtime) of anonymous types,
CodePudding user response:
Used to have written a similar function, in EF preservation before removing the state of the Modified a few fields,
context. Entry (item). State=System. Data. The Entity. The EntityState. Modified; The context. RemoveModifiedStatus (item, c=& gt; New {Arthur c. heckUserId, Arthur c. heckTime, c.P ublishTime, c.G uid});
CodePudding user response:
Implementation method, mainly analytic expression tree, write not familiar to the expression tree, implementation way go detour, but hasn't changed, refer to:
public static void RemoveModifiedStatus (this DbContext context, TSource model, Expression KeySelector) Where TSource: class { //expression Var expression=(new List ()) AsQueryable () OrderBy (keySelector) The Expression;
//expression definition //root Nodes [1]. The Nodes [0]. Nodes [0]. Nodes ExpressionTreeNode root=ExpressionTreeBuilder. GetExpressionTreeNode (expression); If (root==null | | root. The Nodes [1]==null | | root. The Nodes [1]. The Nodes [0]==null | | root. The Nodes [1]. The Nodes [0]. Nodes [0]==null | | root. The Nodes [1]. The Nodes [0]. Nod
This method to rely on a tripartite library ExpressionTreeBuilder, very troublesome,
CodePudding user response:
In fact, parsing keySelector need not so complicated, just directly resolve its Body attribute, refer to the following method, debugged, parsing out from the Body, the things you want to be:
public static string GetPropName (Expression Expr) { The switch (expr. Body. NodeType) { Case ExpressionType. MemberAccess: Return ((MemberExpression expr). The Body). Member. The Name; Case ExpressionType. Convert: Return ((MemberExpression) (the) (UnaryExpression expr) Body). The Operand). Member. The Name; Case ExpressionType. New: //new analytical here {} return null; Default: return null; } }
CodePudding user response:
Using json or XML, ought to be good
CodePudding user response:
CSDN images can no longer click to view the original image, to narrow the pictures don't look! Put your source code, (CSDN for source code display also become garbage blackbody, lost the style of the source code, CSDN is more and more with the color of the chaos, icon, not important text columns)