//可在后面加上语句 .count();会立即执行,不会延迟执行
using System; using System.Linq; using System.Collections.Generic; using System.Text; using System.Threading.Tasks; using System.Data; namespace Dome_2 { class Program { static void Main(string[] args) { linqDome5(); } #region linq查询机制 #region public static void LinqDome6(){ } #endregion #endregion #region linq 相关练习 #region linq的方法对字符串的处理 public static void linqDome6(){ string[] nums = {"张三","李四","李达","查看及时答复","程思佳","非法","成吉思汗","我不是"}; var list = nums.Where(x=>x.Length==2).Select(x=>x).GroupBy(x=>x.Substring(0,1)); foreach(var num in list) { Console.WriteLine("姓====>"+num.Key); foreach(var x in num){ Console.WriteLine(x); } } } #endregion #region linq的方法对字符串的处理和排序 public static void linqDome5(){ string[] nums = {"张三","李四","车嘎达","车嘎新","查看及时答复","程思佳打击非法","成吉思汗","我不是"}; var list = nums.Where(x=>x.Length==3).Select(x=>x).GroupBy(x=>x.Substring(0,1)); foreach(var num in list) { Console.WriteLine("姓====>"+num.Key); foreach(var x in num){ Console.WriteLine(x); } } } #endregion #region linq的方法OrderBy排序 public static void linqDome4(){ int[] nums = {1,88,6,7,2,6,5,4,9,13,20}; //var list = nums.Where(x=>x%2==0).Select(i=>i*i).OrderByDescending(x=>x); 降序 var list = nums.Where(x=>x%2==0).Select(i=>i*i).OrderBy(x=>x); //升序 foreach(var num in list) { Console.WriteLine(num); } } #endregion #region linq的方法where public static void linqDome3(){ int[] nums = {1,7,2,6,5,4,9,13,20}; var list = nums.Where(x=>x%2==0); foreach(var num in list) { Console.WriteLine(num); } } #endregion #region linq的方法select求数组每个数的平方 public static void linqDome2(){ int[] nums = {1,7,2,6,5,4,9,13,20}; var list = nums.Select(x=>x*x); foreach(var num in list) { Console.WriteLine(num); } } #endregion #region Linq的基础练习(初识) public static void LinqDome(){ int[] nums = {1,7,2,6,5,4,9,13,20}; var list = from num in nums where num%2!=0 orderby num descending select num; foreach(var num in list) { Console.WriteLine(num); } Console.WriteLine("Hello World!"); } #endregion #endregion } }