using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Data;namespace LINQtest
{classProgram{publicclassStudent//声明student类{publicint stId;//学生IDpublicstring stuName;//学生姓名}publicclassCourse//声明course类{publicint stId;//学生IDpublicstring courseName;//课程名}static Student[] students =newStudent[]{newStudent{stId =1,stuName ="jack"},newStudent{stId =2,stuName ="taylor"},newStudent{stId =3,stuName ="fleming"}};static Course[] courses =newCourse[]{newCourse{stId =1,courseName ="art"},newCourse{stId =2, courseName ="art"},newCourse{stId =1,courseName ="history"},newCourse{stId =3, courseName ="history"},newCourse{stId =3,courseName ="physics"},};staticvoidMain(string[] args){//查询所有选修了history课的学生名var query =from s in students //从学生列表中选择符合条件的Studentjoin c in courses on s.stId equals c.stId //连接到课程列表中满足条件的Coursewhere c.courseName =="history"//连接到课程列表中满足条件的Courseselect s.stuName;foreach(string str in query){
Console.WriteLine($"{str} "+"选修了历史课");}
Console.ReadKey();}}}
let 应用
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Data;namespace LINQtest
{classProgram{staticvoidMain(string[] args){int[] groupA =newint[]{1,2,3,45,6};int[] groupB =newint[]{11,12,8,9,10};var query2 =from a in groupA
from b in groupB
let sum = a + b
where sum <12selectnewint[]{ a, b, sum };foreach(var str in query2){foreach(var str2 in str){
Console.Write($"{str2}"+"..");}
Console.WriteLine("");}
Console.ReadKey();}}}
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Data;namespace LINQtest{ class Program { public class Student //声明student类 { public in