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 LinqTest
{
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<School> Schools = GetSchools();
//inner join
var query = from a in Students
join b in Schools on a.SchoolID equals b.SchoolID into c
from b in c.DefaultIfEmpty()
select new
{
StudentID = a.StudentID,
StudentName = a.StudentName,
SchoolID = b.SchoolID,
SchoolName = b.SchoolName
};
//where
query = query.Where(s => s.SchoolID != 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", 3));
return students;
}
/// <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"));
schools.Add(new School(3, "school3"));
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 student.
/// </summary>
public class Student
{
public Student() { }
public Student(int _studentID, string _studentName, int _schoolID)
{
this.StudentID = _studentID;
this.StudentName = _studentName;
this.SchoolID = _schoolID;
}
public int StudentID { get; set; }
public string StudentName { get; set; }
public int SchoolID { get; set; }
}
}
List<En_GiveLesson> giveLessonList = list[0].GiveLesson;
List<En_Teacher_InfoModel> teacherList = new En_Teacher_InfoDAL().GetAll();
var resultList = giveLessonList.Join(teacherList, a => a.TeacherID, b => b.Id.ToString(), (a, b) => new
{
a.PlanID,
a.PlanNO,
a.PlanName,
a.Year,
a.Semester,
a.SemesterName,
a.IsCurrentSemester,
a.TeacherName,
a.TeacherID,
a.CreateUserId,
a.CreateUserName,
CreateDate = MongoDBDate.GetLocalDate(a.CreateDate),
a.GiveStudent,
b.Status
}).Where(u => u.PlanNO.Contains(param.PlanNO)
|| u.PlanName.Contains(param.PlanName)
|| u.TeacherName.Contains(param.TeacherName)
|| u.Year.Contains(param.Year)
|| u.SemesterName.Contains(param.SemesterName)).Where(u => u.Status.Equals("1"));