zoukankan      html  css  js  c++  java
  • 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 ? "成年" : "年轻";
            }

  • 相关阅读:
    stata如何建立交互项
    PSM的stata实现
    Stata结果输出:outreg2命令详解
    stata固定效应
    【C++/Qt】cmake生成可执行文件流程(Windows/Linux)
    【数学相关】概率论手写笔记
    【搜索】dfs&bfs
    【C/C++】对拍教程
    【MySQL】MySQL基础操作(下)
    【Linux】ubuntu初始配置
  • 原文地址:https://www.cnblogs.com/selfimprove/p/3602774.html
Copyright © 2011-2022 走看看