c# linq 多表内联实例

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;


namespace WebApplication29
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindStudents();
            }
        }
        /// <summary>
        /// Binding the filter list of students.
        /// </summary>
        private void BindStudents()
        {
            GridView1.DataSource = GetFilterStudents();
            GridView1.DataBind();
        }
        /// <summary>
        /// Get the filter list of students.
        /// </summary>
        /// <returns></returns>
        private IList GetFilterStudents()
        {
            List<Student> students = GetStudents();
            List<Grade> grades=GetGrades();
            List<School> schools = GetSchools();
            //inner join
            var query = from a in students
                        join b in grades on a.GradeID equals b.GradeID into ab
                        from b in ab.DefaultIfEmpty()
                        join c in schools on b.SchoolID equals c.SchoolID into bc
                        from c in bc.DefaultIfEmpty()
                        select new
                        {
                            StudentID=a.StudentID,
                            StudentName=a.StudentName,
                            GradeName=b.GradeName,
                            SchoolName=c.SchoolName
                        };
            //where
            query = query.Where(s => s.StudentID != 1);
            //order by
            query = query.OrderByDescending(s => s.StudentID);
            return query.ToList();
        }
        /// <summary>
        /// Get the list of students.
        /// </summary>
        /// <returns></returns>
        private List<Student> GetStudents()
        {
            List<Student> students = new List<Student>();
            students.Add(new Student(1, "student1", 1));
            students.Add(new Student(2, "student2", 1));
            students.Add(new Student(3, "student3", 2));
            students.Add(new Student(4, "student4", 2));
            students.Add(new Student(5, "student5", 3));
            students.Add(new Student(6, "student6", 4));
            return students;
        }
        /// <summary>
        /// Get the list of grades
        /// </summary>
        /// <returns></returns>
        private List<Grade> GetGrades()
        {
            List<Grade> grades = new List<Grade>();
            grades.Add(new Grade(1, "grade1", 1));
            grades.Add(new Grade(2, "grade2", 1));
            grades.Add(new Grade(3, "grade1", 2));
            grades.Add(new Grade(4, "grade2", 2));
            return grades;
        }
        /// <summary>
        /// Get the list of schools.
        /// </summary>
        /// <returns></returns>
        private List<School> GetSchools()
        {
            List<School> schools = new List<School>();
            schools.Add(new School(1, "school1"));
            schools.Add(new School(2, "school2"));
            return schools;
        }
    }
    /// <summary>
    /// Class of school.
    /// </summary>
    public class School
    {
        public School() { }
        public School(int _schoolID, string _schoolName)
        {
            this.SchoolID = _schoolID;
            this.SchoolName = _schoolName;
        }
        public int SchoolID { get; set; }
        public string SchoolName { get; set; }
    }
    /// <summary>
    /// Class of grade.
    /// </summary>
    public class Grade
    {
        public Grade() { }
        public Grade(int _gradeID,string _gradeName,int _schoolID)
        {
            this.GradeID = _gradeID;
            this.GradeName = _gradeName;
            this.SchoolID = _schoolID;
        }
        public int GradeID { get; set; }
        public string GradeName { get; set; }
        public int SchoolID { get; set; }
    }
    /// <summary>
    /// Class of student.
    /// </summary>
    public class Student
    {
        public Student() { }
        public Student(int _studentID, string _studentName, int _gradeID)
        {
            this.StudentID = _studentID;
            this.StudentName = _studentName;
            this.GradeID = _gradeID;
        }
        public int StudentID { get; set; }
        public string StudentName { get; set; }
        public int GradeID { get; set; }
    }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值