public class Classroom
    {
        /// <summary>
        /// 班级Id
        /// </summary>
        public int ClassId { get; set; }
        /// <summary>
        /// 班级名称
        /// </summary>
        public string ClassName { get; set; }

    }
    public class Student
    {
        /// <summary>
        /// 学生编号
        /// </summary>
        public int StuId { get; set; }
        /// <summary>
        /// 学生姓名
        /// </summary>
        public string StuName { get; set; }

        /// <summary>
        /// 班级ID
        /// </summary>
        public int ClassId { get; set; }

        /// <summary>
        /// 班级
        /// </summary>
        public Classroom Classrooms { get; set; }

    }
    public class Score
    {
        /// <summary>
        /// 分数Id
        /// </summary>
        public int ScoreId { get; set; }
        /// <summary>
        /// 学科名称
        /// </summary>
        public string ItemName { get; set; }
        /// <summary>
        /// 学生ID
        /// </summary>
        public int StuId { get; set; }
        /// <summary>
        /// 学生
        /// </summary>
        public Student Students { get; set; }
        /// <summary>
        /// 分数
        /// </summary>
        public double Scores { get; set; }
    }
   
    public class Program
    {
        
       

        static void Main()
        {
            List<Student> lstStudent = new List<Student>
            {
                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<Classroom> lstClassRoom = new List<Classroom>
            {
                new Classroom{ ClassId=1, ClassName="class1"},
                new Classroom{ ClassId=2, ClassName="class2"},
            };
            List<Score> lstScores = new List<Score>
            {
                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<int, Student> mySortlist = new SortedList<int, Student>
            {
                {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<T>,要用LINQ查询则必须用Cast方法进行转换
            var str = from m in strs.Cast<int>() select m;
            string stra = "What's your Name";
            var capChars = from m in stra
                           let a=(int)m
                           where a==97
                           select m;

 

 
posted on 2013-04-27 15:52  赶路人之刚出发  阅读(424)  评论(0编辑  收藏  举报