• Linq无聊练习系列2--select/distinct练习


    void dataBindByWhere()
            {

                /**************select/distinct 练习*******************/
                //获取数据库中的T_Student表数据
                var list = from s in ctx.T_Student
                           select s;
                //采用匿名类型
                var list1 = from s in ctx.T_Student
                            select new
                            {
                                stuNumber = s.stuNumber,
                                stuSex = s.stuSex,
                                //这里边是可以计算的
                                stuAge = s.stuAge / 2 + s.stuAge / 2,
                                //也可以进行条件判断
                                stuAgeTime = s.stuAge>20?"成年":"年轻",
                                stuName = s.stuName
                            };
                //也可以不用匿名类型获取个别字段值
                var list2 = from s in ctx.T_Student
                            select s.stuName;
                //也可以转化为自定义的类型,比如这里的peple为自定义的类型
                var list3 = from s in ctx.T_Student
                            select new People
                                {
                                    stuNumber = s.stuNumber,
                                    stuName = s.stuName
                                };
                //可以在select 匿名类型里边继续包括匿名类型
                var list4 = from s in ctx.T_Student
                            select new
                            {

                                stuNumber = s.stuNumber,
                                stuName = s.stuName,
                                stuInfo = new T_Student { stuAge = s.stuAge, stuSex = s.stuSex }
                            };
                //也可以继续在select 里边包括select
                var list5 = from s in ctx.T_Student
                            select new {
                                stuNumber = s.stuNumber,
                                stuSCore = from c in s.T_Score
                                           where s.stuNumber==c.stuNumber
                                           select c.score
                            };
                //也可以在select 里边调用方法
                var list6 = from s in ctx.T_Student
                            select new {
                                stuNumber = s.stuNumber,
                                stuName = s.stuName,
                                stuAge = AgeInfo(s.stuAge)
                            };
                //也可以对查询结果去除重复
                var list7 = (from s in ctx.T_Student
                             select s.stuName).Distinct();
                GridView1.DataSource = list;
                GridView1.DataBind();
            }
            string AgeInfo(int age)
            {
                return age > 20 ? "成年" : "年轻";
            }

  • 相关阅读:
    break,continue,return的区别
    java中for循环的优化
    Jquery中click函数调用遇到的诡异问题
    MVC中Controller与View的类型绑定问题
    基于8019芯片的在9S12下移植成功的TCP/IP协议族(续)
    MVC中Partialiew使用的一点小总结
    基于8019芯片的在9S12下移植成功的TCP/IP协议族(一)
    9S12单片机的模块驱动程序备忘
    prim算法(zoj1203)
    win7下安装fedora
  • 原文地址:https://www.cnblogs.com/selfimprove/p/3602774.html
Copyright © 2020-2023  润新知