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 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; }
    }
}


你可能感兴趣的:(C#,LINQ,内联)