Home > Net >  Dynamically build a Lambda
Dynamically build a Lambda

Time:03-12

CodePudding user response:

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)

CodePudding user response:

refer to the original poster specialized in a variety of odd jobs response:



 
# Region "dynamic construction of the Selector expressions"
# Region "BuildSelectorExpression"
"' & lt; Summary>
"' dynamically build the Selector expression returns the query expression
"' & lt;/summary>
"' & lt; Typeparam name="T" & gt;
"' & lt; Param name="sources" & gt;
"' & lt; Param name="fields" & gt;
"' & lt; Returns>

The Public Function SelectDynamic (Of T) (sources As IEnumerable (Of T), fields As IEnumerable (Of String)) As IEnumerable (Of Object)
Return sources. AsQueryable. Select (BuildSelectorExpression (Of T) (fields))
End the Function
"' & lt; Summary>
"' build the Selector expressions
"' & lt;/summary>
"' & lt; Param name="fields" & gt; Need to select the attribute name & lt;/param>
"' & lt; Returns> The Selector expressions & lt;/returns>

The Public Function BuildSelectorExpression (Of T) (fields As IEnumerable (Of String)) As Expression (Of Func (Of T, Object))
Dim Properties_dic As New Dictionary (Of String, PropertyInfo) (GetType (T) GetRuntimeProperties. Where (Function (p) fields. The Contains (p.N ame)), ToDictionary (Function (p) p.N ame, Function (p) p))
Dim DynamicType As Type=LinqRuntimeTypeBuilder. GetDynamicType (Properties_dic. Values)
Dim the Parameter As ParameterExpression=Expression. The Parameter (GetType (T), "p")
Dim MemberBindings As IEnumerable (Of MemberBinding)=DynamicType. GetRuntimeProperties (). The Select (Function (p) Expression. The Bind (p, Expression. The Property (Parameter, Properties_dic p.N (ame)))). The OfType (Of MemberBinding) ()
Return Expression. Lambda (Of Func (Of T, Object)) (Expression. MemberInit (Expression. [New] (DynamicType. GetConstructor (Type. EmptyTypes)), MemberBindings), the Parameter)
End the Function
# End Region
# Region "LinqRuntimeTypeBuilder"
Public NotInheritable Class LinqRuntimeTypeBuilder
Private Shared ReadOnly AssemblyName As New AssemblyName () With {. Name="LinqRuntimeTypes"}
Private Shared ReadOnly ModuleBuilder As ModuleBuilder
Private Shared ReadOnly BuiltTypes_dic As New Dictionary (Of String, Type) ()
Private Const RuntimeGetSetAttrs As MethodAttributes=MethodAttributes. Public Or MethodAttributes. SpecialName Or MethodAttributes. HideBySig
Shared Sub New ()
ModuleBuilder=AssemblyBuilder. DefineDynamicAssembly (AssemblyName, AssemblyBuilderAccess. Run). DefineDynamicModule (AssemblyName. Name)
End Sub

"' & lt; Summary>
"' BuildDynamicType
"' & lt;/summary>
"' & lt; Param name="properties_dic & gt;"
"' & lt; Returns>
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  •  Tags:  
  • LINQ
  • Related