开发手册 欢迎您!
软件开发者资料库

.NET(C#) Linq Where和Select的使用

Linq是Language Integrated Query的简称,它是微软在.NET Framework 3.5里面新加入的特性,用以简化查询查询操作。本文主要介绍.NET(C#) 中Linq的Where和Select操作符。

1、Where操作符

Where操作符用于限定输入集合中的元素,将符合条件的元素组织声称一个序列结果。

例如,

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks; namespace ConsoleApplication{    class Program    {        static void Main(string[] args)        {            List listInt = new List();     //List类实现了IEnumerable接口,因此它也可以使用IEnumerable的扩展方法            listInt.Add(1);            listInt.Add(4);            listInt.Add(5);            listInt.Add(12);            listInt.Add(15);            //外部定义方法为委托赋值            IEnumerable IEnum1 = listInt.Where(GetBiggerThanTen);            foreach (int i in IEnum1)            {                Console.WriteLine(i);                                    }            Console.WriteLine("==============================");            //匿名方法为委托赋值            IEnumerable IEnum2 = listInt.Where(            delegate (int input)            {                if (input > 10)                {                    return true;                }                return false;            });            foreach (int i in IEnum2)            {                Console.WriteLine(i);               }            Console.WriteLine("==============================");            //Lambda表达式为委托赋值            IEnumerable IEnum3 = listInt.Where(m => m > 10);            foreach (int i in IEnum3)            {                Console.WriteLine(i);               }            Console.WriteLine("==============================");            var Arr = listInt.Where(m => m > 10);            foreach (var i in Arr)            {                Console.WriteLine(i);                          Console.ReadKey();            }        }        public static bool GetBiggerThanTen(int input)        {            if (input > 10)            {                return true;            }            return false;        }    }}

2、Select操作符

Select操作符用于根据输入序列中的元素创建相应的输出序列中的元素,输出序列中的元素类型可以与输入序列中的元素类型相同,也可以不同。

例如,

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks; namespace ConsoleApplication{       class Program    {        static void Main(string[] args)        {            List pList = new List();            People p1 = new People(1, "C", 21);            People p2 = new People(1, "Java", 22);            People p3 = new People(1, "Python", 23);            pList.Add(p1);            pList.Add(p2);            pList.Add(p3);            IEnumerable IEP = pList.Select(p => p.Name);    //查询的是元素的姓名属性            foreach (string str in IEP)            {                Console.WriteLine(str);     //输出C Java Python            }                                            //此处必须使用var,因为是匿名类型,无法写成IEnumerable的形式                var newList = pList.Select((m, i) => new { index = i, m.Name });    //输入元素与下标,返回一个新的匿名类型的集合                foreach (var item in newList)                {                    Console.Write(item.Name);   //输出 CJavaPython            }                    Console.ReadKey();                }            }        }        public class People        {            public People(int id, string name, int age)            {                this.Id = id;                this.Name = name;                this.Age = age;            }            public int Id            {                get;                set;            }            public string Name            {                get;                set;            }            public int Age            {                get;                set;            }        }    }}