Lambda Expression in C #

Source: Internet
Author: User

1.Expression

expression<func<double, double>> exp = a = Math.sin (a);

The delegate type func<double, Double>, which qualifies the resulting expression tree to be a unary lambda function that accepts a double and returns a double

<func<double, double, double, double, double> the input parameter is 4 double, which returns a double type
  static void Main (string[] args)    {        expression<       func<  Console.WriteLine (Calc. Calculate (1, 2, 3, 4)); }

2. Use in LINQ to SQL

LoadWith method, input type is generic T, input parameter is expression, return System.Linq.Expressions.LambdaExpression

Loadswith method, accept lambdaexpression[], load data

  <func<t, object> input generic, returns object (because the return type is undefined, so it is an object)
    public partial class Db//for db.dbml filespartial class 
{

public static LambdaExpression loadwith<t> (expression<func<t, object>> Expression)
{
return expression;
}

public void Loadswith (params lambdaexpression[] expressions)
{
var dlo = new DataLoadOptions ();
foreach (var expression in expressions)
{
Dlo. LoadWith (expression);
}
Loadoptions = Dlo;
}

public static Db Open (bool readOnly = False) {

var db = new db ();

if (readOnly) db. objecttrackingenabled = false;

return DB;

}
}

LoadWith is interpreted on MSDN by using a lambda expression to retrieve the specified data related to the primary target. LoadWith can load data from related tables with an SQL statement

For example student table inner JOIN class table, this is the meaning of loadwith.

Call:

using (var db = Db.open ()) {        db. Loadswith (          db.loadwith<Project> (p = p.client),           db.loadwith<Project> (p = p.files)        );         var project = db. Projects.singleordefault (p = = P.id = = form. ID);}

exploring Lambda Expression in C #

Expression Tree Getting Started Guide

Https://msdn.microsoft.com/en-us/library/bb397687.aspx

use expression expressions to resolve queries similar to select in SQL (for Small to medium projects)

LINQ to SQL

Lambda Expression in C #

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.