语法糖(Frm_07)

using Students.BLL;
using Students.Modal;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Students
{
    public partial class Frm_07 : Form
    {
        StudentBLL studentBLL = new StudentBLL();
        List<StudentModal> studentModalList = null;

        public Frm_07()
        {
            InitializeComponent();
            studentModalList = studentBLL.FindStudentsAll();
        }

        private void btn_All_Click(object sender, EventArgs e)
        {
            var queryList = (from stu in studentModalList select stu).ToList();
            dgv.DataSource = queryList;
        }

        private void btn_Query1_Click(object sender, EventArgs e)
        {
            var queryList = (from stu in studentModalList
                             where stu.Sname == txt_QueryName.Text.Trim()
                             select stu).ToList();
            dgv.DataSource = queryList;
        }

        private void btn_Query2_Click(object sender, EventArgs e)
        {
            var queryList = (from stu in studentModalList
                             where stu.Sname == txt_QueryName.Text.Trim() && stu.Sage
                             == int.Parse(txt_QueryAge.Text.Trim())
                             select stu).ToList();
            dgv.DataSource = queryList;
        }

        private void btn_Query3_Click(object sender, EventArgs e)
        {
            int index = txt_QueryAge.Text.Trim().Length > 0 ? int.Parse(txt_QueryAge.Text.Trim()) : 0;
            var queryList = (from stu in studentModalList
                             where stu.Sname.Equals(txt_QueryName.Text.Trim()) ||
                             stu.Sage == index
                             select stu).ToList();
            dgv.DataSource = queryList;
        }

        private void btn_Query4_Click(object sender, EventArgs e)
        {
            var queryList = (from stu in studentModalList
                             where stu.Sname.Contains(txt_QueryName.Text.Trim())
                             select stu).ToList();
            dgv.DataSource = queryList;
        }

        private void btn_QueryOrderBy_Click(object sender, EventArgs e)
        {
            var queryList = (from stu in studentModalList
                             where stu.Sname.Contains(txt_QueryName.Text.Trim())
                             orderby stu.Sname descending
                             select stu).ToList();
            dgv.DataSource = queryList;
        }

        private void btn_QueryGroupBy_Click(object sender, EventArgs e)
        {
            var queryList = (from stu in studentModalList
                             group stu by stu.Sage).ToList();
            dgv.DataSource = queryList;
        }

        private void btn_Max_Click(object sender, EventArgs e)
        {
            //var queryList = studentModalList.Max(x => x.Sage);
            var queryList = (from stu in studentModalList
                             orderby stu.Sage descending
                             select stu).Take(1).ToList();
            dgv.DataSource = queryList;
        }

        private void btn_Avg_Click(object sender, EventArgs e)
        {
            var queryList = studentModalList.Average(x => x.Sage);
        }

        private void btn_Sum_Click(object sender, EventArgs e)
        {
            var queryList = studentModalList.Sum(x => x.Sage);
        }

        int page = 1;
        private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            page = 1;
            var pageList = (from stu in studentModalList
                            orderby stu.Sname descending
                            select stu).Skip((page - 1) * 5)
                           .ToList().Take(5).ToList();
            dgv.DataSource = pageList;
        }

        private void linkLabel2_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            page = page - 1 > 1 ? page - 1 : 1;
            var pageList = (from stu in studentModalList
                            orderby stu.Sname descending
                            select stu).Skip((page - 1) * 5)
                           .ToList().Take(5).ToList();
            dgv.DataSource = pageList;
        }

        private void linkLabel3_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            int maxpage = studentModalList.Count() % 5 == 0 ? studentModalList.Count() / 5 : (studentModalList.Count() / 5) + 1;
            page = page + 1 < maxpage ? page + 1 : maxpage;
            var pageList = (from stu in studentModalList
                            orderby stu.Sname descending
                            select stu).Skip((page - 1) * 5)
                           .ToList().Take(5).ToList();
            dgv.DataSource = pageList;
        }

        private void linkLabel4_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            page = studentModalList.Count() % 5 == 0 ? studentModalList.Count() / 5 : (studentModalList.Count() / 5) + 1;
            var pageList = (from stu in studentModalList
                            orderby stu.Sname descending
                            select stu).Skip((page - 1) * 5)
                           .ToList().Take(5).ToList();
            dgv.DataSource = pageList;
        }

        private void btn_DataTable_Click(object sender, EventArgs e)
        {

            dgv.DataSource = studentBLL.FindStudentsAllDataTable(new StudentModal()
            {
                Sname = txt_QueryName.Text.Trim()
            });

            //DataTable dt = studentBLL.FindStudentsAllDataTable();
            dt.AsEnumerable() 将datayable 转换成为一个简单的迭代器的泛型结合
            stu.Field<string>("sname") <当前需要作为查询条件的字段的数据类型>(字段的名称)
            //var queryList = (from stu in dt.AsEnumerable()
            //                 where stu.Field<string>("sname").Contains
            //                 (txt_QueryName.Text.Trim())
            //                 select stu).ToList();
            //List<StudentModal> studentModalList = new List<StudentModal>();
            //foreach (var item in queryList)
            //{
            //    studentModalList.Add(new StudentModal()
            //    {
            //        Sname = item.Field<string>("sname"),
            //        Sid = item.Field<Guid>("sid")
            //    });
            //}
            // dgv.DataSource = queryList;
        }

        private void btn_join_Click(object sender, EventArgs e)
        {
            List<StudentModal> studentModalList = new StudentBLL().FindStudentsAll();
            List<GradeModal> gradeModalList = new GradeBLL().FindGradeAll();
            var queryList = (from stu in studentModalList
                             where stu.Sname.Contains(txt_QueryName.Text.Trim())
                             join gra in gradeModalList
                             on stu.gid equals gra.Gid
                             select new
                             {
                                 stuName = stu.Sname,
                                 stuGname = gra.Gname,
                                 stuAge = stu.Sage
                             }).ToList();
            dgv.DataSource = queryList;
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值