Home Backend Development C#.Net Tutorial Detailed explanation of examples of anonymous delegates and Lambda expressions in C#

Detailed explanation of examples of anonymous delegates and Lambda expressions in C#

Jun 23, 2017 pm 02:49 PM
.net lambda Anonymous entrust expression

1. C# from 1.0 to 4.0, with the support of Linq and generics, the code becomes more and more simple and elegant

 1 int[] nums = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 }; 2             IEnumerable<int> newNums = from n in nums where n > 0 select n; 3             newNums = newNums.Where(new Func<int,int, bool>(delegate(int i,int index) { return i < index; })); 4             newNums = newNums.Where(new Func<int, int, bool>((int i, int index)=> i < index)); 5             newNums = newNums.Where(delegate(int i, int index) { return i < index; }); 6             newNums = newNums.Where((i, index) => i < index); 7             foreach (var i in newNums) 8             { 9                 Console.WriteLine(i);10             }
Copy after login

2. Set operations can also be adapted to EF Database operations

1. Create two entity classes

 1     public class Store 2     { 3         public string Id; 4         public string Name; 5     } 6     public class Person 7     { 8         public string name { get; set; } 9         public int age { get; set; }10         public string StoreId { get; set; }11     }
Copy after login

2. Insert data

 1             var Stores = new List<Store>() 2             { 3                 new Store() { Id="1",Name="1班"}, 4                 new Store() { Id="2",Name="2班"} 5             }; 6  7             var Persons = new List<Person>() 8             { 9                 new Person() { name="p1",age=1, StoreId="1"},10                 new Person() { name="p2",age=2, StoreId="1"},11                 new Person() { name="p3",age=3, StoreId="1"},12                 new Person() { name="p4",age=4, StoreId="2"},13                 new Person() { name="p5",age=5, StoreId="1"},14                 new Person() { name="p6",age=6, StoreId="2"},15                 new Person() { name="p7",age=7, StoreId="1"},16                 new Person() { name="p8",age=8, StoreId="1"}17             };
Copy after login

3. Query age How many people are younger than 3 years old in Class 1 and Class 2 respectively

1             var plst = Persons.Where(o => o.age > 3).GroupBy(o => o.StoreId).Select(g => new { StoreId = g.Key, Count = g.Count() }).Join(Stores, s => s.StoreId, p => p.Id, (s, p) => new { s.StoreId, storeName = p.Name, s.Count });2             foreach (var p in plst)3             {4                 Console.WriteLine(p.storeName + "有" + p.Count + "个人");5             }
Copy after login

4. Output

2班有2个人
1班有3个人
Copy after login

The above is the detailed content of Detailed explanation of examples of anonymous delegates and Lambda expressions in C#. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Two Point Museum: All Exhibits And Where To Find Them
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How do lambda expressions handle exceptions in C++? How do lambda expressions handle exceptions in C++? Apr 17, 2024 pm 12:42 PM

In C++, there are two ways to handle exceptions using Lambda expressions: catch the exception using a try-catch block, and handle or rethrow the exception in the catch block. Using a wrapper function of type std::function, its try_emplace method can catch exceptions in Lambda expressions.

What is the meaning of closure in C++ lambda expression? What is the meaning of closure in C++ lambda expression? Apr 17, 2024 pm 06:15 PM

In C++, a closure is a lambda expression that can access external variables. To create a closure, capture the outer variable in the lambda expression. Closures provide advantages such as reusability, information hiding, and delayed evaluation. They are useful in real-world situations such as event handlers, where the closure can still access the outer variables even if they are destroyed.

What are the advantages of using C++ lambda expressions for multi-threaded programming? What are the advantages of using C++ lambda expressions for multi-threaded programming? Apr 17, 2024 pm 05:24 PM

The advantages of lambda expressions in C++ multi-threaded programming include simplicity, flexibility, ease of parameter passing, and parallelism. Practical case: Use lambda expressions to create multi-threads and print thread IDs in different threads, demonstrating the simplicity and ease of use of this method.

How to implement closure in C++ Lambda expression? How to implement closure in C++ Lambda expression? Jun 01, 2024 pm 05:50 PM

C++ Lambda expressions support closures, which save function scope variables and make them accessible to functions. The syntax is [capture-list](parameters)->return-type{function-body}. capture-list defines the variables to capture. You can use [=] to capture all local variables by value, [&] to capture all local variables by reference, or [variable1, variable2,...] to capture specific variables. Lambda expressions can only access captured variables but cannot modify the original value.

Share several .NET open source AI and LLM related project frameworks Share several .NET open source AI and LLM related project frameworks May 06, 2024 pm 04:43 PM

The development of artificial intelligence (AI) technologies is in full swing today, and they have shown great potential and influence in various fields. Today Dayao will share with you 4 .NET open source AI model LLM related project frameworks, hoping to provide you with some reference. https://github.com/YSGStudyHards/DotNetGuide/blob/main/docs/DotNet/DotNetProjectPicks.mdSemanticKernelSemanticKernel is an open source software development kit (SDK) designed to integrate large language models (LLM) such as OpenAI, Azure

What are the employment prospects of C#? What are the employment prospects of C#? Oct 19, 2023 am 11:02 AM

Whether you are a beginner or an experienced professional, mastering C# will pave the way for your career.

How does a C++ lambda expression capture external variables? How does a C++ lambda expression capture external variables? Apr 17, 2024 pm 04:39 PM

There are three ways to capture lambda expressions of external variables in C++: Capture by value: Create a copy of the variable. Capture by reference: Get a variable reference. Capture by value and reference simultaneously: Allows capturing of multiple variables, either by value or by reference.

How to perform lazy evaluation using C++ lambda expressions? How to perform lazy evaluation using C++ lambda expressions? Apr 17, 2024 pm 12:36 PM

How to perform lazy evaluation using C++ lambda expressions? Use lambda expressions to create lazily evaluated function objects. Delayed computation defers execution until needed. Calculate results only when needed, improving performance.

See all articles