using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace demo_linq_select { class Program { static void Main(string[] args) { int[] a = new int[] {1, 2, 3, 4, 5}; //select 为必选关键字 为前面满足条件的选择输出 返回结果为返回IEnumerable<TSOURCE>泛型接口 IEnumerable<int> result = from r in a where r < 3 select r; Console.WriteLine(result.ToList()); foreach (var t in result) { Console.WriteLine(t); } //匿名对象 var obj = new { name = "leo", sex = "male" }; Console.WriteLine(obj.name); Console.WriteLine(obj.sex); //LINQ高级扩展方式 var result1 = a.Where(t => t < 3).Select(r => r); //where参数FUNC<TSOURCE, TRESULT>委托, 这里Select不是必须, //使用select实现对查询结果进行过滤,转换为一个新的对象等 foreach (var t in result1) { Console.WriteLine(t); } var result2 = a.First(); Console.WriteLine(result2); //Skip 跳过指定数量的元素 var result3 = a.Skip(2).Where(r => r >= 4); print<int>(result3); //联合查询 var b = new int[] {3, 4, 5, 6}; var result4 = from c in a from d in b where c == d select c + d; print<int>(result4); Console.ReadKey(); } static void print<T>(IEnumerable<T> result) { foreach (var t in result) { Console.WriteLine(t); } } } }