Entity Framework beginners (1.5)-lambda expressions and

Source: Internet
Author: User
Tags microsoft c

After the EF model is created, do not worry about using it. before using it, you also need to understand two related technologies, lambda expressions and LINQ.

As an important syntax in Microsoft C #, sugar-lambda expressions and LINQ are essentially a method. Taking lambda expressions as an example:

X => x + 1; is a complete Lambda expression.

It is equivalent to an anonymous method:

Int anonymous (int x)

{

Return x + 1;

}

Consider the most important part of this method, that is, two parts: the input parameter, the returned parameter.

The Return Value Type and parameter types can be inferred, just like the VaR keyword in C. Therefore, x => x + 1 is the simplest implementation of X ++ lambda expressions.

Knowing its nature, how should we use it? Let me give you the simplest example: retrieve all strings starting with S from an irregular string array.

Here, the most direct and minimum white statement is as follows:

Loop through the entire array, find the string whose s index is 0, and add it to the new list.

In addition to this, there are some more elegant and object-oriented methods, such:

However, we have written a loop to create a new set to collect all the variables that meet the conditions. What if I use Lambda or LINQ?

This section includes the creation of metadata, filtering using two methods, and finally printing to the screen.

The first sentence is to create metadata, without detailed explanation, and the second sentence is the writing of the LINQ statement.

From X in XXX Where true select X is a typical sentence of LINQ. X is a temporary variable, which is the same as X in lambda expressions. XXX is an enumerative type, such as an array or a set. Where is followed by a bool value used as the judgment condition. The final result after select is the return value. We return X and finally use the toarray method. Therefore, the final data type is string []. (If you use the tolist () method, the collection class list will be obtained. <string> ).

The third sentence is lambda writing. Where () indicates the query condition. If you need to explicitly specify the retrieved value, you can add a. Select (x => X) statement after the where method ). Of course, it is unnecessary to take only one value here.

 

After reading this example, do you want to ask, does this have a gross relationship with Ef?

In fact, this example is used in database queries, which is equivalent to select ID from Table1 where name like's % ';

This example can be changed to the most important query method in EF.

 

Entity Framework beginners (1.5)-lambda expressions and

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.