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 ? "成年" : "年轻";
            }

  • 相关阅读:
    Codeforces 691A Fashion in Berland
    HDU 5741 Helter Skelter
    HDU 5735 Born Slippy
    HDU 5739 Fantasia
    HDU 5738 Eureka
    HDU 5734 Acperience
    HDU 5742 It's All In The Mind
    POJ Euro Efficiency 1252
    AtCoder Beginner Contest 067 C
    AtCoder Beginner Contest 067 D
  • 原文地址:https://www.cnblogs.com/selfimprove/p/3602774.html
Copyright © 2011-2022 走看看