首页 > 编程语言 >C#的LINQ select查询、where过滤、group分组、join关联

C#的LINQ select查询、where过滤、group分组、join关联

时间:2022-10-29 11:35:06浏览次数:74  
标签:group name C# age List LINQ student Student new


语言集成查询 (Language Integrated Query) 是一系列直接将查询功能集成到 C# 语言的技术统称,用于在所有基于.NET平台的变成语言中更加直接地声明跨越、过滤和投射操作的一种方式,标准查询操作允许查询作用于所有基于IEnumerable接口的源。

LINQ简单查询

using System;
using System.Linq;

namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
int[] scores = new int[] { 90, 91, 59, 55, 80 };
var x = from score in scores where score >= 60 orderby score descending select score;
foreach( var s in x)
{
Console.WriteLine(s);
}
Console.ReadLine();
}
}
}

C#的LINQ select查询、where过滤、group分组、join关联_匿名类型

函数

案例一

using System;
using System.Collections.Generic;
using System.Linq;

namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
List<int> num1 = new List<int> { 1, 2, 3, 4, 5 };
List<int> num2 = new List<int> { 17, 18, 19, 20 };
var concat = num1.Concat(num2);
double average = concat.Average();
Console.WriteLine(average);
Console.ReadLine();
}
}
}

案例二

using System;
using System.Collections.Generic;
using System.Linq;

namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
int[] scores = new int[] { 81, 90, 60, 55, 100 };
var c = from score in scores where score >= 60 select score;
Console.WriteLine(c.Count());
Console.WriteLine(c.Max());
Console.ReadLine();
}
}
}

LINQ子句

where子句

using System;
using System.Collections.Generic;
using System.Linq;

namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
List<Student> students = new List<Student>()
{
new Student() {name="张三", age=22, telephone="1234567890"},
new Student() {name="张四", age=23, telephone="1234567891"},
new Student() {name="王五", age=21, telephone="1234567892"}
};

var query = from student in students
where student.name.Contains("张")
&& student.age > 21
select student;
foreach (var s in query)
{
Console.WriteLine("{0} - {1} - {2}", s.name, s.age, s.telephone);
}
Console.ReadLine();
}
}



public class Student
{
public string name { get; set; }
public int age { get; set; }
public string telephone { get; set; }
}
}

C#的LINQ select查询、where过滤、group分组、join关联_.net_02

orderby 子句

using System;
using System.Collections.Generic;
using System.Linq;

namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
List<Student> students = new List<Student>()
{
new Student() {name="张三", age=22, telephone="1234567890"},
new Student() {name="张四", age=23, telephone="1234567891"},
new Student() {name="王五", age=21, telephone="1234567892"}
};

var query = from student in students
where student.name.Contains("张")
&& student.age > 21
orderby student.age descending
select student;
foreach (var s in query)
{
Console.WriteLine("{0} - {1} - {2}", s.name, s.age, s.telephone);
}
Console.ReadLine();
}
}



public class Student
{
public string name { get; set; }
public int age { get; set; }
public string telephone { get; set; }
}
}

C#的LINQ select查询、where过滤、group分组、join关联_.net_03

select 子句

using System;
using System.Collections.Generic;
using System.Linq;

namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
List<Student> students = new List<Student>()
{
new Student() {name="张三", age=22, telephone="1234567890"},
new Student() {name="张四", age=23, telephone="1234567891"},
new Student() {name="王五", age=21, telephone="1234567892"}
};
// 替换
var query1 = from student in students
orderby student.age descending
select student.name.Replace("张四", "李四");
// 自定义方法
var query2 = from student in students
orderby student.age descending
select MyFunc(student.name);
// 匿名类型
var query3 = from student in students
orderby student.age descending
select new { name=student.name + "OK!", age=student.age+10};
// 对查询结果投影
var query4 = from student in students
orderby student.age descending
select new Student{ name = student.name, age = 25, telephone=""};
foreach (var s in query1)
{
Console.WriteLine(s);
}
foreach (var s in query2)
{
Console.WriteLine(s);
}
foreach (var s in query3)
{
Console.WriteLine(s.name + " " + s.age);
}
foreach (var s in query4)
{
Console.WriteLine(s.name + " " + s.age);
}
static string MyFunc(string s)
{
return s + "好!";
}
Console.ReadLine();
}
}



public class Student
{
public string name { get; set; }
public int age { get; set; }
public string telephone { get; set; }
}
}

C#的LINQ select查询、where过滤、group分组、join关联_数据源_04

多个from子句

using System;
using System.Collections.Generic;
using System.Linq;

namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
List<Student> students = new List<Student>()
{
new Student() {name="张三", age=22, addr= new List<string>(){ "中国", "山东"}, scores=new List<int>{99, 100} },
new Student() {name="张四", age=23, addr= new List<string>(){ "中国", "山东"}, scores=new List<int>{99, 100}},
new Student() {name="王五", age=21, addr= new List<string>(){ "中国", "山东"}, scores=new List<int>{99, 100}}
};
var query1 = from student in students
from add in student.addr
from score in student.scores
orderby student.age descending
select new {newName = student.name, newAddr = add, nawSocre = score};
foreach(var s in query1)
{
Console.WriteLine(s);
}
Console.ReadLine();
}
}



public class Student
{
public string name { get; set; }
public int age { get; set; }
public List<string> addr { get; set; }
public List<int> scores { get; set; }
}
}

LINQ中的from子句中引用的数据源类型必须是IEnumerable、IEnumerable或一种派生类型(如IQueryable)类型。

如果一个数据源里面又包含一个或多个集合列表,可以使用复合的from子句查询。

C#的LINQ select查询、where过滤、group分组、join关联_c#_05

group子句

using System;
using System.Collections.Generic;
using System.Linq;

namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
List<Student> students = new List<Student>()
{
new Student() {name="张三", age=22, addr= new List<string>(){ "中国", "山东"}, scores=new List<int>{99, 100} },
new Student() {name="张四", age=23, addr= new List<string>(){ "中国", "山东"}, scores=new List<int>{99, 100}},
new Student() {name="王五", age=21, addr= new List<string>(){ "中国", "北京"}, scores=new List<int>{99, 100}}
};
var query = from student in students
group student by student.addr[1];
foreach(var group in query)
{
Console.WriteLine(group.Key);
foreach(var s in group)
{
Console.WriteLine(" "+s.name);
}
}
Console.ReadLine();
}
}



public class Student
{
public string name { get; set; }
public int age { get; set; }
public List<string> addr { get; set; }
public List<int> scores { get; set; }
}
}

此例使用双层循环根据省份进行分组。group查询产生的类型是IGrouping<TKey, TElement>类型,实质是列表的列表。

C#的LINQ select查询、where过滤、group分组、join关联_匿名类型_06

into子句

into子用于group、select、join子句中充当其结果引用的临时标识符。

using System;
using System.Collections.Generic;
using System.Linq;

namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
List<Student> students = new List<Student>()
{
new Student() {name="张三", age=22, addr= new List<string>(){ "中国", "山东"}, scores=new List<int>{99, 100} },
new Student() {name="张四", age=23, addr= new List<string>(){ "中国", "山东"}, scores=new List<int>{90, 80}},
new Student() {name="王五", age=21, addr= new List<string>(){ "中国", "北京"}, scores=new List<int>{99, 80}}
};
var query = from student in students
group student by student.scores[1] into student_group
orderby student_group.Key descending
select student_group;
foreach(var group in query)
{
Console.WriteLine(group.Key);
foreach(var s in group)
{
Console.WriteLine(" "+s.name);
}
}
Console.ReadLine();
}
}



public class Student
{
public string name { get; set; }
public int age { get; set; }
public List<string> addr { get; set; }
public List<int> scores { get; set; }
}
}

C#的LINQ select查询、where过滤、group分组、join关联_数据源_07

let 子句

let子句产生一个用于存储查询表达式中的子表达式查询结果的范围变量。

using System;
using System.Collections.Generic;
using System.Linq;

namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
List<Student> students = new List<Student>()
{
new Student() {name="张三", age=22, addr= new List<string>(){ "中国", "山东"}, scores=new List<int>{99, 100} },
new Student() {name="张四", age=23, addr= new List<string>(){ "中国", "山东"}, scores=new List<int>{90, 80}},
new Student() {name="王五", age=21, addr= new List<string>(){ "中国", "北京"}, scores=new List<int>{99, 80}}
};
var query = from student in students let c = student.addr
where c[0]=="中国" && c[1]=="山东"
select student;
// 等效where
var query1 = from student in students
where student.addr[0] == "中国" && student.addr[1] == "山东"
select student;
foreach (var s in query)
{
Console.WriteLine(s.name);
}
foreach (var s in query1)
{
Console.WriteLine(s.name);
}
Console.ReadLine();
}
}



public class Student
{
public string name { get; set; }
public int age { get; set; }
public List<string> addr { get; set; }
public List<int> scores { get; set; }
}
}

C#的LINQ select查询、where过滤、group分组、join关联_匿名类型_08

join子句

join子句可以关联两个数据源,使用equals关键字进行比较两个数据源中的某个属性,而不是用“==”。

using System;
using System.Collections.Generic;
using System.Linq;

namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
List<Student> students1 = new List<Student>()
{
new Student() {name="张三", age=22},
new Student() {name="张四", age=23},
new Student() {name="王五", age=21}
};
List<Student> students2 = new List<Student>()
{
new Student() {name="张三", scores=new List<int>{99, 100}},
new Student() {name="张四", scores=new List<int>{90, 80}},
new Student() {name="王五", scores=new List<int>{99, 80}}
};
var query = from student1 in students1
join student2 in students2
on student1.name equals student2.name
select new Student {name=student1.name,
age=student1.age, scores=student2.scores};
foreach (var s in query)
{
Console.WriteLine(string.Format("Name:{0}, Age:{1}, Scores:{2}",
s.name, s.age, s.scores[0]));
}
Console.ReadLine();
}
}



public class Student
{
public string name { get; set; }
public int age { get; set; }
public List<int> scores { get; set; }
}
}

C#的LINQ select查询、where过滤、group分组、join关联_c#_09

参考

​https://learn.microsoft.com/zh-cn/dotnet/csharp/programming-guide/concepts/linq/​


标签:group,name,C#,age,List,LINQ,student,Student,new
From: https://blog.51cto.com/lilongsy/5805842

相关文章

  • Java通过jdbc连接MySql数据库进行操作
    下载​​MySql​​驱动包​​https://dev.mysql.com/downloads/connector/j/​​解压,把文件夹中的​​jar​​包拷贝到项目文件中新建一个java类进行连接​​db1​​是我......
  • Scanner类
    1、需要传参:System.in2、scanner.next()接收字符串类型3、scanner.nextInt()接收整型packagecom.ke.demo1;importjava.util.Scanner;publicclassdemo1{......
  • [Vue warn]: Method “components“ has type “object“ in the component definitio
    [Vuewarn]:Method“components”hastype“object”inthecomponentdefinition.Didyoureferencethe报错如下:仔细检查代码发现:components写到methods方法里面去......
  • 云小课|MRS基础原理之CarbonData入门
    阅识风云是华为云信息大咖,擅长将复杂信息多元化呈现,其出品的一张图(云图说)、深入浅出的博文(云小课)或短视频(云视厅)总有一款能让您快速上手华为云。更多精彩内容请单击......
  • 贝叶斯推理三种方法:MCMC 、HMC和SBI
    对许多人来说,贝叶斯统计仍然有些陌生。因为贝叶斯统计中会有一些主观的先验,在没有测试数据的支持下了解他的理论还是有一些困难的。本文整理的是作者最近在普林斯顿的一个......
  • Mockito使用方法(Kotlin)
    一、为什么要使用Mockito1.实际案例1.1遇到的问题对于经常维护的项目,经常遇到一个实际问题:需求不停改变,导致架构经常需要修改某些概念的定义。对于某些十分基础又十分......
  • C/C++ struct结构体的三种使用方法
    #include<stdio.h>#include<string.h>#include<stdlib.h>#include<iostream>#include<conio.h>usingnamespacestd;structhero{charname[16];ch......
  • 基于C语言的通用型数据结构与容器库
    仓库地址:github:https://github.com/hellototoro/hlibcgitee:https://gitee.com/totorohello/hlibclist双向序列容器,用于将它们的元素保持为线性排列,并允许在序列的任何......
  • C语言“我的家谱”程序
    C语言“我的家谱”程序建立“我的家谱”问题家谱:又称族谱,是记载一个家族的世系繁衍及重要人物事迹的书。家谱中记录着父亲、母亲和孩子的姓名。请大家利用二叉树设计一个......
  • unity 使用动画器覆盖控制器(AnimatorOverrideController)快速创建新对象的动画控制器
    注释:假设你已经创建好了一个怪物对象的基础动画控制,此时需要在添加一个全新的敌人,你又懒得从新写一堆参数和代码,那么就可以使用这种重写控制器来快速生成控制器参数则使......