• Linq无聊练习系列4--join练习


    /**************join 练习*******************/
                //对于1对多关系
                var list =from c in ctx.T_Student
                          from s in c.T_Score
                          where c.stuName=="黄阳"
                          select s;
                //也可以间接的通过表关联其它表,这个结果和上边的查询结果相同
                var list1 = from s in ctx.T_Score
                            where s.T_Student.stuName=="黄阳"
                            select s;
                //多对多关系,他们之间的关系是1:M:1
                var list2 = from s in ctx.T_Cource
                            from c in s.T_Score
                            select new {
                                s.courceName,
                                c.score,
                                c.T_Student.stuName
                            };
                //如果表里边有隶属关系,可以使用自联结,由于表的设计不是很好,所以没有包括这种情况
                //双向链接
                var list3 = from s in ctx.T_Student
                            join t in ctx.T_Score
                            on s.stuNumber
                            equals t.stuNumber
                            into g
                            select new {
                               stuName =  s.stuName,
                               score = g.Sum(k=>k.score)               
                            };
                //三向链接
                var list4 = from s in ctx.T_Score
                            join c in ctx.T_Cource
                            on s.courceNumber
                            equals c.courceNumber into temp1
                            join t in ctx.T_Student
                            on s.stuNumber
                            equals t.stuNumber into temp2
                            select new {
                               Score = s.score,
                               Name = temp1.Count()
                            };
                //左外部链接
                var list5 = from s in ctx.T_Student
                            join c in ctx.T_Score
                            on s.stuNumber
                            equals c.stuNumber
                            into temp1
                            from k in temp1.DefaultIfEmpty()
                            select new
                            {
                                stuName = s.stuName,
                                sumScore = k.score
                            };
                //投影的let操作
                var list6 = from s in ctx.T_Student
                            join c in ctx.T_Score
                            on s.stuNumber
                            equals c.stuNumber into temp1
                            let studentInfo = s.stuName +s.stuSex
                            from k in temp1
                            select new {
                                stuNumber=s.stuNumber,
                                CoreOfStudents = k.score,
                                studentInfomation = studentInfo
                            };
                    //使用组合健来表示多表之间的关系 ,这个由于表中设计时候,基本只有一个主键,没有用到联合主键  
                //一侧可为空另一侧不能为空的链接
                var list7 = from s in ctx.T_Student
                            join c in ctx.T_Score
                            on Convert.ToInt32(s.stuNumber)
                            equals (int ?)Convert.ToInt32(c.stuNumber) into temp1
                            select new {
                                stuName = s.stuNumber,
                                sumCore = temp1.Sum(f=>f.score)
                            };

  • 相关阅读:
    作为【开发人员】如何持续提升自己的开发技能
    永远不要放弃做梦的权利---与所有程序员们共勉
    十种更好的表达“你的代码写的很烂”的方法---总有些人的代码让人难以忍受
    程序员技术练级攻略--练成这样,成神仙了!
    创业其实是个逻辑问题![想不想创业都来看看]
    多图震撼!数字的未来,2013报告
    记最难忘的一件事 等笑话一箩筐
    HDU4666 Hyperspace(曼哈顿)
    POJ3436 ACM Computer Factory(最大流)
    再思考
  • 原文地址:https://www.cnblogs.com/selfimprove/p/3603384.html
Copyright © 2020-2023  润新知