0
点赞
收藏
分享

微信扫一扫

c#LINQ、Lambda总结


where筛选方法

筛选出集合中的所有偶数并取平方

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

namespace Linq
{
class Program
{
static void Main(string[] args)
{

List<int> list = new List<int>() {1,2,3,4,5,6,7,8,9,10 };
var list1 = list.Where(x => x % 2 == 0).Select(y => y*y );
foreach(var item in list1)
{
Console.WriteLine(item);
}
}

}
}

c#LINQ、Lambda总结_c#

2、First

List<string> strList = new List<string>()
{
"ABC",
"123456",
"哈哈",
"12121212"
};
//LinQ 的 First函数,a:是strList数组中的元素,如果元素的长度大于5;
//—— First 函数会找到满足条件,最近的一个
string result = strList.First(a => a.Length > 5);
// //Lambda 表达式的返回值是根据你的函数决定的,
//在此是string类型的返回值,所以可以直接写一句
Console.WriteLine(strList.First(a=>a.Length>5));//

c#LINQ、Lambda总结_linq_02


举报

相关推荐

Linq和C# Lambda表达式

C# LINQ,SQL

C# Linq介绍

[C#] LINQ之GroupBy

LINQ详解二(C#)

.NET(C#) Linq 简介

windows C#-LINQ查询

0 条评论