網站首頁 編程語言 正文
C# 匿名函數、lambda表達式、Linq查詢
一、匿名函數的使用
匿名函數是一個“內聯”語句或表達式,可在需要委托類型的任何地方使用。
可以使用匿名函數來初始化命名委托,或傳遞命名委托(而不是命名委托類型)作為方法參數。
下面的示例演示了從 C# 1.0 到 C# 3.0 委托創建過程的發展:
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表達式
lambda表達式是一個匿名函數,是一種高效的類似于函數式編程的表達式,Lambda簡化了開發中需要編寫的代碼量,是LINQ的基礎。
lambda表達式格式:(參數列表)=>表達式或語句塊 ,舉例如下:
//無參
() => DoSomeThing() ;
//單參數
p => p.id > 0 ; //返回Bool
//多參數
( x , y ) => x * y ;
//帶類型輸入參數
( int x , int y ) => x * y;
下面介紹List集合中的Lambda表達式的運用:
(1) 查詢班級編號為1001的班級下面的所有學生實體并返回到list1001中存儲
var list1001=Studentlist.Where(t=>t.ClassCode==‘1001');
(2) 查詢班級編號為1001的班級下面的所有學生實體并返回到list1001中存儲,并按照學生的出生日期從小到大排列。
var list1001=Studentlist.Where(t=>t.ClassCode==‘1001').OrderBy(t=>t.BirthDay);
在此說一下,OrderBy是從小到大排序,需要從大到小排列則用OrderByDescending。
(3) 查詢班級編號為1001的班級下面的姓氏為【李】的同學的所有集合,并按照學生的出生日期從小到大排列。
var list1001=Studentlist.Where(t=>t.ClassCode==‘1001'&&t.StudentName.StartWith(“李”)).OrderBy(t=>t.BirthDay);
(4) 查詢出班級編號為1001的班級,并且存在至少一門考試科目成績低于60分的所有同學。
var result = studentList.Where(t => (t.ClassCode == "1001") && (scoreList.Exists(p => p.ScoreValue < 60 && p.StudentCode == t.StudentCode)));
(5) 其他較常用的Lambda表達式如下:
var a = studentList.FirstOrDefault(t => t.StudentCode == "10012");//FirstOrDefault返回第一個符合條件的數據,不存在的時候返回Null。
var b = studentList.Count(t => t.StudentName == "李世民");//返回符合條件的實體個數
var c = studentList.FindAll(t => t.StudentName.Contains("中"));//查找所有名字中含有【中】的實體集合
var d = studentList.GroupBy(t => t.ClassCode);//對studentList按照ClassCode分組
var f = studentList.Max(t => t.BirthDay);//返回最大的出生日期。
var e = scoreList.Sum(t => t.ScoreValue);//對所有成績求和
var g = scoreList.Average(t => t.ScoreValue);//對所有成績求平均分
var h = studentList.Select(t => t.StudentName).Distinct();//獲取所有的學生姓名,并去除重名
總結
原文鏈接:https://blog.csdn.net/qq_43024228/article/details/107035662
相關推薦
- 2022-04-23 Linux基于客戶端用戶密鑰登錄服務端用戶
- 2023-02-12 JetpackCompose?Navigation導航實現流程_Android
- 2022-09-25 React組件的生命周期函數
- 2021-04-07 Websocket免費在線測試請求工具
- 2022-05-17 Spring boot 集成Redis客戶端Lettuce,導致服務線程數不斷增加
- 2022-08-15 gitlab代碼合并(merge request )取消 [默認刪除分支(Delete source
- 2022-03-25 Redis分布式鎖如何實現續期_Redis
- 2022-06-29 python密碼學庫pynacl功能介紹_python
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支