博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
LINQ join/left join/cross join/group by/group join/sortedlist/cast
阅读量:5093 次
发布时间:2019-06-13

本文共 3911 字,大约阅读时间需要 13 分钟。

 
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()        {            List
lstStudent = 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;
 

 

 

转载于:https://www.cnblogs.com/Finding2013/archive/2013/04/27/3047296.html

你可能感兴趣的文章
Screening technology proved cost effective deal
查看>>
【POJ2699】The Maximum Number of Strong Kings(网络流)
查看>>
spring boot配置跨域
查看>>
BZOJ 1996 合唱队(DP)
查看>>
进击吧!阶乘——大数乘法
查看>>
安卓学习资料推荐-25
查看>>
Mysql数据库备份和还原常用的命令
查看>>
关于退出当前页面在火狐的一些问题
查看>>
【项目实施】项目考核标准
查看>>
spring-aop AnnotationAwareAspectJAutoProxyCreator类
查看>>
经典入门_排序
查看>>
Redis Cluster高可用集群在线迁移操作记录【转】
查看>>
二、spring中装配bean
查看>>
VIM工具
查看>>
javascript闭包
查看>>
创建本地yum软件源,为本地Package安装Cloudera Manager、Cloudera Hadoop及Impala做准备...
查看>>
mysql8.0.13下载与安装图文教程
查看>>
url查询参数解析
查看>>
http://coolshell.cn/articles/10910.html
查看>>
[转]jsbsim基础概念
查看>>