Obtain the value of the lambda expression.

Source: Internet
Author: User

With the prevalence of LINQ, more and more expressions are used for LINQ and lmabda. as proposed in Net Framework 3.5, lambda expressions are an anonymous method, which is usually used to create delegation in LINQ. However, when we use lmabda expressions to implement some operations, it is very important to break down the internal expression tree structure. For example, we need to directly use t => T in some method calls. the name method gets the attribute name, so that we don't have to write the string ourselves, and the compiler can help us refactor and detect the attribute when it changes.

When we want to implement the above method, we will not only use anonymous delegation, but also use expression, which is located in system. LINQ. in the expressions namespace, you can find the specific information in msdn, which is not listed here. {Class. the format of attribute} is a memberexpression object, and the node type is memberaccess. Because the types of the attributes to be obtained are not necessarily the same, therefore, the Delegate for retrieving attribute names can only be defined as func <t, Object>. The Code is as follows:

public static string ResloveName<T>(Expression<Func<T, object>> expression)
{
    var exp = expression.Body as MemberExpression;
    string expStr = exp.ToString();
    return expStr.Substring(expStr.IndexOf(".") + 1);
}

The following code is available:

User user = new User { Name = "Xiao Ming" };
Expression<Func<User, bool>> exp = u => u.Name == user.Name;

If we want to use this Binary Expression to construct an SQL statement, how should we break down this expression? My approach is to first convert the subject of the expression to binaryexpression, then, determine whether the expressions in the left and right attributes are the same as the exp parameter expressions, and calculate the values of different expressions, used as a parameter. The Code is as follows:

BinaryExpression binaryExp = exp.Body as BinaryExpression;
Expression constantExp = (binaryExp.Left as MemberExpression).Expression == exp.Parameters[0] ? binaryExp.Right : binaryExp.Left;
string value = Expression.Lambda(constantExp).Compile().DynamicInvoke().ToString();

From the inheritance system, we can find that all generic expressions are inherited from lambdaexpression. Therefore, we can reload a method with the following code:

public static string ResloveName(Expression<Func<T, object>> expression)
{
    return ResloveName(expression as LambdaExpression);
}

public static string ResloveName(LambdaExpression expression)
{
    var exp = expression.Body as MemberExpression;
    string expStr = exp.ToString();
    return expStr.Substring(expStr.IndexOf(".") + 1);
}

I hope the above Code can help you in the application of expressions. Thank you!

Transferred from:Ahl5esoft.

Obtain the value of the lambda expression.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.