linq用法

片段一

            class Student
            {
                public string name;
                public int id;
                //班级
                public string className;
            }

            Student[] arr =
            {
                new Student {name = "zhangshan", id = 0, className = "二班"},
                new Student {name = "lisi", id = 1, className = "二班"},
                new Student {name = "wanger", id = 2, className = "一班"},
                new Student {name = "wangsan", id = 3, className = "二班"},
                new Student {name = "wangsi", id = 4, className = "三班"},
                new Student {name = "wangwuc", id = 5, className = "四班"},
                new Student {name = "wangliub", id = 6, className = "五班"},
                new Student {name = "wangqib", id = 7, className = "二班"},
                new Student {name = "wangba", id = 8, className = "六班"},
                new Student {name = "wangjiu", id = 9, className = "三班"},
                new Student {name = "wangshi", id = 10, className = "二班"}
            };

            //写法一
            //var rst = from s in arr where s.name.StartsWith("wang") 
            orderby  s.name[s.name.Length - 1] select s.name;

            //写法二
            var rst = arr.Where(s => s.name.StartsWith("wang")).
            Select(s => s.name).OrderBy(s=>s[s.Length-1]);

            System.Console.WriteLine("数目{0}", rst.Count());

            foreach (var VARIABLE in rst)
            {
                System.Console.WriteLine(VARIABLE);
            }

选择以wang打头的字符串,用最后一个字母排序,运行结果,

片段二

选择两列内容输出

        class Student
        {
            public string name;
            public int id;
            //班级
            public string className;
        }

        static void Main(string[] args)
        {
            Student[] arr =
            {
                new Student {name = "zhangshan", id = 0, className = "二班"},
                new Student {name = "lisi", id = 1, className = "二班"},
                new Student {name = "wanger", id = 2, className = "一班"},
                new Student {name = "wangsan", id = 3, className = "二班"},
                new Student {name = "wangsi", id = 4, className = "三班"},
                new Student {name = "wangwuc", id = 5, className = "四班"},
                new Student {name = "wangliub", id = 6, className = "五班"},
                new Student {name = "wangqib", id = 7, className = "二班"},
                new Student {name = "wangba", id = 8, className = "六班"},
                new Student {name = "wangjiu", id = 9, className = "三班"},
                new Student {name = "wangshi", id = 10, className = "二班"}
            };

            var rst = from s in arr where s.name.StartsWith("wang") 
            select new {s.name, s.className};

            System.Console.WriteLine("数目{0}", rst.Count());

            foreach (var VARIABLE in rst)
            {
                System.Console.WriteLine("name = {0}, className = {1}",
                 VARIABLE.name, VARIABLE.className);
            }

            System.Console.ReadLine();
        }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值