欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C#中的匿名函數(shù)、lambda表達(dá)式解讀

 更新時(shí)間:2023年01月24日 14:53:41   作者:Danny_hi  
這篇文章主要介紹了C#中的匿名函數(shù)、lambda表達(dá)式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

C# 匿名函數(shù)、lambda表達(dá)式、Linq查詢

一、匿名函數(shù)的使用

匿名函數(shù)是一個(gè)“內(nèi)聯(lián)”語句或表達(dá)式,可在需要委托類型的任何地方使用。

可以使用匿名函數(shù)來初始化命名委托,或傳遞命名委托(而不是命名委托類型)作為方法參數(shù)。

下面的示例演示了從 C# 1.0 到 C# 3.0 委托創(chuàng)建過程的發(fā)展:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Test0630
{
? ? delegate void TestDelegate(string s);
? ? class Program
? ? {
? ? ? ? static void Main(string[] args)
? ? ? ? {
? ? ? ? ? ? // Original delegate syntax required?
? ? ? ? ? ? // initialization with a named method.
? ? ? ? ? ? TestDelegate testDelA = new TestDelegate(M);

? ? ? ? ? ? // C# 2.0: A delegate can be initialized with
? ? ? ? ? ? // inline code, called an "anonymous method." This
? ? ? ? ? ? // method takes a string as an input parameter.
? ? ? ? ? ? TestDelegate testDelB = delegate(string s) { Console.WriteLine(s); };

? ? ? ? ? ? // C# 3.0. A delegate can be initialized with
? ? ? ? ? ? // a lambda expression. The lambda also takes a string
? ? ? ? ? ? // as an input parameter (x). The type of x is inferred by the compiler.
? ? ? ? ? ? TestDelegate testDelC = (x) => { Console.WriteLine(x); };

? ? ? ? ? ? // Invoke the delegates.
? ? ? ? ? ? testDelA("Hello,this is TestA");
? ? ? ? ? ? testDelB("Hello,this is TestB");
? ? ? ? ? ? testDelC("Hello,this is TestC");

? ? ? ? ? ? // Keep console window open in debug mode.
? ? ? ? ? ? Console.WriteLine("Press any key to exit.");
? ? ? ? ? ? Console.ReadKey();
? ? ? ? }


? ? ? ? static void M(string s)
? ? ? ? {
? ? ? ? ? ? Console.WriteLine(s);
? ? ? ? }
? ? }
}

二、lambda表達(dá)式

lambda表達(dá)式是一個(gè)匿名函數(shù),是一種高效的類似于函數(shù)式編程的表達(dá)式,Lambda簡(jiǎn)化了開發(fā)中需要編寫的代碼量,是LINQ的基礎(chǔ)。

lambda表達(dá)式格式:(參數(shù)列表)=>表達(dá)式或語句塊 ,舉例如下:

//無參
() => DoSomeThing() ;

//單參數(shù)
p => p.id > 0 ; //返回Bool

//多參數(shù)
( x , y ) => x * y ;

//帶類型輸入?yún)?shù)
( int x , int y ) => x * y;

下面介紹List集合中的Lambda表達(dá)式的運(yùn)用:

(1) 查詢班級(jí)編號(hào)為1001的班級(jí)下面的所有學(xué)生實(shí)體并返回到list1001中存儲(chǔ)

var list1001=Studentlist.Where(t=>t.ClassCode==‘1001');

(2) 查詢班級(jí)編號(hào)為1001的班級(jí)下面的所有學(xué)生實(shí)體并返回到list1001中存儲(chǔ),并按照學(xué)生的出生日期從小到大排列。

var list1001=Studentlist.Where(t=>t.ClassCode==‘1001').OrderBy(t=>t.BirthDay);

在此說一下,OrderBy是從小到大排序,需要從大到小排列則用OrderByDescending。

(3) 查詢班級(jí)編號(hào)為1001的班級(jí)下面的姓氏為【李】的同學(xué)的所有集合,并按照學(xué)生的出生日期從小到大排列。

var list1001=Studentlist.Where(t=>t.ClassCode==‘1001'&&t.StudentName.StartWith(“李”)).OrderBy(t=>t.BirthDay);

(4) 查詢出班級(jí)編號(hào)為1001的班級(jí),并且存在至少一門考試科目成績(jī)低于60分的所有同學(xué)。

var result = studentList.Where(t => (t.ClassCode == "1001") && (scoreList.Exists(p => p.ScoreValue < 60 && p.StudentCode == t.StudentCode)));

(5) 其他較常用的Lambda表達(dá)式如下:

var a = studentList.FirstOrDefault(t => t.StudentCode == "10012");//FirstOrDefault返回第一個(gè)符合條件的數(shù)據(jù),不存在的時(shí)候返回Null。
var b = studentList.Count(t => t.StudentName == "李世民");//返回符合條件的實(shí)體個(gè)數(shù)
var c = studentList.FindAll(t => t.StudentName.Contains("中"));//查找所有名字中含有【中】的實(shí)體集合
var d = studentList.GroupBy(t => t.ClassCode);//對(duì)studentList按照ClassCode分組
var f = studentList.Max(t => t.BirthDay);//返回最大的出生日期。
var e = scoreList.Sum(t => t.ScoreValue);//對(duì)所有成績(jī)求和
var g = scoreList.Average(t => t.ScoreValue);//對(duì)所有成績(jī)求平均分
var h = studentList.Select(t => t.StudentName).Distinct();//獲取所有的學(xué)生姓名,并去除重名

總結(jié)

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論