对于一个集合,只要该集合实现了System.Collections.IEnumerable接口,或者是泛型接口:System.Collections,Generic.IEnumerable<T>,就可以使用LINQ查询该集合数据.

using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace LinqObject{    class Program    {        static void Main(string[] args)        {            Int32[] myNnums = new int[] { 1, 4, 9, 16, 22, 25, 56, 79 };            //System.Linq.Enumerable.WhereArrayIterator
everyNums = from p in myNnums var everyNums = from p in myNnums where (p % 2) == 0 select p; Console.WriteLine("结果:"); foreach (int val in everyNums) { Console.WriteLine(val.ToString()); } } }}

二:查询对象列表

1,建model:

using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace LinqObject.models{    public class PlayerInfoVO    {        private string name;        public string Name        {            get { return name; }            set { name = value; }        }        private uint id;        public uint Id        {            get { return id; }            set { id = value; }        }        private uint level;        public uint Level        {            get { return level; }            set { level = value; }        }    }}

2,建IEnumerable<PlayerInfoVO>

using System;using System.Collections.Generic;using System.Linq;using System.Text;using LinqObject.listmodel;namespace LinqObject.listmodel{    public class PlayerList    {        private static PlayerList myInstance;        public static PlayerList Instance() {            if (myInstance == null) myInstance = new PlayerList();            return myInstance;        }        public PlayerList() {            if (myInstance != null)            {                throw new Exception("PlayerList 被设计成 单例!!!");            }            else {                myInstance = this;            }        }        public IEnumerable
CreatePlayerInfoVo() { return new List
{ new models.PlayerInfoVO{ Id=1, Name = "Ainy" , Level = "1"}, new models.PlayerInfoVO{ Id=2, Name = "Aonaufly" , Level = "2"} }; } }}改写

Main

using System;using System.Collections.Generic;using System.Linq;using System.Text;using LinqObject.listmodel;using LinqObject.models;namespace LinqObject{    class Program    {        static void Main(string[] args)        {            Int32[] myNnums = new int[] { 1, 4, 9, 16, 22, 25, 56, 79 };            //System.Linq.Enumerable.WhereArrayIterator
everyNums = from p in myNnums var everyNums = from p in myNnums where (p % 2) == 0 select p; Console.WriteLine("结果:"); foreach (int val in everyNums) { Console.WriteLine(val.ToString()); } var result = from c in PlayerList.Instance().CreatePlayerInfoVo() where c.Name == "Aonaufly" select c; Console.WriteLine(result); } }}