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

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值