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; }
}
}
c# linq 多表内联实例
最新推荐文章于 2022-06-07 16:29:05 发布