public class Classroom { ////// 班级Id /// public int ClassId { get; set; } ////// 班级名称 /// public string ClassName { get; set; } } public class Student { ////// 学生编号 /// public int StuId { get; set; } ////// 学生姓名 /// public string StuName { get; set; } ////// 班级ID /// public int ClassId { get; set; } ////// 班级 /// public Classroom Classrooms { get; set; } } public class Score { ////// 分数Id /// public int ScoreId { get; set; } ////// 学科名称 /// public string ItemName { get; set; } ////// 学生ID /// public int StuId { get; set; } ////// 学生 /// public Student Students { get; set; } ////// 分数 /// public double Scores { get; set; } } public class Program { static void Main() { ListlstStudent = new List { new Student{StuId=1,StuName="stu1",ClassId=1}, new Student{StuId=2,StuName="stu2",ClassId=2}, new Student{StuId=4,StuName="stu3",ClassId=2} }; List lstClassRoom = new List { new Classroom{ ClassId=1, ClassName="class1"}, new Classroom{ ClassId=2, ClassName="class2"}, }; List lstScores = new List { new Score{ ItemName="Chinese", ScoreId=1, StuId=1, Scores=90}, new Score{ ItemName="Chinese2", ScoreId=2, StuId=2,Scores=80}, new Score{ ItemName="Chinese3", ScoreId=3, StuId=5,Scores=80} }; //内连接 var query1=lstStudent.Join(lstScores, stu => stu.StuId, score => score.StuId, (stu, score) => new { name = stu.StuName, Item = score.ItemName, scores = score.Scores }); //内连接2 query1 = from a in lstStudent join b in lstScores on a.StuId equals b.StuId select new { name = a.StuName, Item = b.ItemName, scores = b.Scores, }; //左链接 query1 = from a in lstStudent.AsEnumerable() join b in lstScores on a.StuId equals b.StuId into temp from m in temp.DefaultIfEmpty(new Score{ StuId=a.StuId , ItemName = "没参考", Scores = 0 }) select new { name=a.StuName, Item = m.ItemName, scores = m.Scores, }; //表A.GroupJoin(表B,表A外键,表B外键,处理方法) //表A中数据全显示,表B关联上的就显示,否则为空,类似于左查询 query1 = lstStudent.GroupJoin(lstScores, m => m.StuId, o => o.StuId, (stu, score) => new { name = stu.StuName, Item = score.First().ItemName, scores = score.First().Scores }); foreach (var item in query1) { } //交叉连接 query1 = from a in lstStudent from b in lstScores select new { name = a.StuName, Item = b.ItemName, scores = b.Scores, }; var tmpQuery1=from a in lstStudent join b in lstScores on a.StuId equals b.StuId select new { classid=a.ClassId, name = a.StuName, Item = b.ItemName, scores = b.Scores, }; //group by var tmpGroupQuery = from a in tmpQuery1 group a by a.classid into sums select new { classid = sums.Key, score = sums.Sum(o => o.scores) }; SortedList mySortlist = new SortedList { { 12,new Student{ ClassId=1, StuId=1, StuName="stu1"}}, { 5,new Student{ ClassId=1, StuId=2, StuName="stu2"}}, { 4,new Student{ ClassId=1, StuId=3, StuName="stu3"}} }; //mySortlist.Add(4, new Student { ClassId = 1, StuId = 3, StuName = "stu6" }); int[] ints = new int[10]; object obj = ints; object[] objarr = obj as object[]; ArrayList strs = new ArrayList { 1, 2, 3, 4, 5 }; //ArrayList实现的事IEnumerable而非IEnumerable ,要用LINQ查询则必须用Cast方法进行转换 var str = from m in strs.Cast () select m; string stra = "What's your Name"; var capChars = from m in stra let a=(int)m where a==97 select m;