LINQ常用语句

1、数组排序
void ArrLinq()
        {
            string temp = null;
            int[] arr = new int[] { 8, 5, 89, 3, 56, 4, 1, 58 };
            var m = from n in arr where n < 58 orderby n select n;
            foreach (var n in m)
            {
                temp += n + ",";
            }
            Label1.Text = temp.ToString();
        }
2、数组排序
void ClassLinq()
        {
            List<Person> list = new List<Person>()
            {
                new Person(){ Name="Olive",Sex="女",Age=22},
                new Person(){ Name="Moyao",Sex="男",Age=23},
                new Person(){ Name="Momo",Sex="女",Age=22},
                new Person(){ Name="Only",Sex="女",Age=20},
                new Person(){ Name="Love",Sex="女",Age=21},
                new Person(){ Name="For",Sex="女",Age=22},
                new Person(){ Name="Remote",Sex="男",Age=23},
                new Person(){ Name="Snow",Sex="女",Age=23}
            };

            //从list集合中选出性别为“女”
            var girls = from g in list
                        where g.Sex == "女"
                        select g;

            //从list集合中选出性别为“男”的人          
            var boys = list.Where(p => p.Sex == "男");

            Console.WriteLine("Girls");
            foreach (var g in girls)
            {
                Console.WriteLine("姓名:" + g.Name + "--性别:" + g.Sex + "--年龄:" + g.Age);
            }

            Console.WriteLine("Boys");
            foreach (var b in boys)
            {
                Console.WriteLine("姓名:" + b.Name + "--性别:" + b.Sex + "--年龄:" + b.Age);
            }
        }

        public class Person
        {
            public string Name
            {
                get;
                set;
            }


            public string Sex
            {
                get;
                set;
            }


            public int Age
            {
                get;
                set;
            }
        }

你可能感兴趣的:(LINQ常用语句)