一、简介:

Linq(语言集成查询): 为 C# 和 Visual Basic 提供语言级查询功能和高阶函数 API,让你能够编写具有很高表达力度的声明性代码。

二、优点:

1、LINQ具有语言级查询语法,切可用let定义语句内变量;

2、具有很高的表达力度。

三、基础语法示例:

// See https://aka.ms/new-console-template for more information
using Microsoft.VisualBasic;
using System;
using System.Linq;

Console.WriteLine("Hello, World!");

Console.WriteLine("示例");
int[] scores = { 97, 92, 81, 60 };

var aa= from score in scores  
        where score > 80  // 查询语法必须以from子句开头,可以以Select或GroupBy子句结束;可以使用过滤,连接,分组,排序运算符以构造所需的结果
        select score;  // 查询语法允许使用 let 子句,这样,便可以在表达式的作用域内引入和绑定变量,然后在表达式的后续片段中使用该变量。 
string aaStr1 = string.Join(",", aa);
Console.WriteLine("查询语法示例: " + aaStr1);

var aa1 = scores.Where(a => a > 80);  // 有Max();Count();ToDictionary(value=>key)等查询方法
string aa1Str2= string.Join(",", aa1);
Console.WriteLine("API语法示例: "+ aa1Str2);

string[] strs = new string[] {"1","2" };
int seeds = 2;
int sumLength = strs.Aggregate(seeds, (seeds, str) => seeds + str.Length);
Console.WriteLine("Aggregate累加方法的结果: " + sumLeng
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.

四、常用API语法:

1、Select():查询

2、 SelectMany():嵌套查询

3、Where():

GroupBy

OrderBy:

ThenBy:

Skip,Take:从第几个开始到第几个结束

SkipWhile()和TakeWhile():满足条件即停止执行

Join,Equals:

Distinct:去重复

Union:合集并;

Intersect:合集交

Except:合集差

Concat():拼接

Contains():包含

Sum():

Max():

Min():

Average:

Aggregate:累加

All():

Any():

Count():

LongCount():返回long类型的参数

ToArray:

ToList:

ToDictionary:

ToLookup:

FirstOrDefault:

LastOrDefault:

作者:꧁执笔小白꧂