您好,登錄后才能下訂單哦!
===========================================Racer.cs
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Collections; namespace ConsoleApplication4 { [Serializable] public class Racer:IComparable<Racer>,IFormattable { public string FirstName { get; private set; }//第一個武將 public string LastName { get; private set; }//第二個武將 public int Wins { get; private set; }//贏得次數 public string Country { get; private set; }//國家 public int Starts { get; private set; }//開始 public string[] Arms { get; private set; }//武器 public int[] Years { get; private set; }//年份 public Racer(string firstname = "", string lasename = "", int wins = 0, string country = "", int starts = 0, IEnumerable<string> Arms = null, IEnumerable<int> years = null) { this.FirstName = firstname; this.LastName = lasename; this.Wins = wins; this.Country = country; this.Starts = starts; List<string> LArms = new List<string>(); foreach (var item in Arms) { LArms.Add(item); } this.Arms = LArms.ToArray(); List<int> Lyears = new List<int>(); foreach (var item in years) { Lyears.Add(item); } this.Years = Lyears.ToArray(); } public int CompareTo(Racer other) { if (other == null) throw new ArgumentNullException("對象不能為空"); return this.Wins.CompareTo(other.Wins); } public string ToString(string format, IFormatProvider formatProvider) { switch (format) { case "": return ToString(); case "C": StringBuilder sb = new StringBuilder(); foreach (var item in Arms) { sb.Append(item + ","); } return sb.ToString().TrimEnd(','); case "Y": StringBuilder sb2 = new StringBuilder(); foreach (var item in Years) { sb2.Append(item + ","); } return sb2.ToString().TrimEnd(','); default: return ToString(); } } public override string ToString() { return string.Format("第一個賽手:{0},最后一個賽手:{1},贏的次數:{2},國家:{3},開始:{4}",this.FirstName,this.LastName,this.Wins.ToString(),this.Country,this.Starts.ToString()); } } }
===========================================Team.cs
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApplication4 { [Serializable] public class Team { public string Name { get; private set; }//團隊名稱 public int[] Years { get; private set; } public Team(string name,params int[] years) { this.Name = name; this.Years = years; } } }
===========================================Formula.cs
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApplication4 { public static class Formula { private static List<Racer> racers; private static List<Team> team; public static IList<Racer> GetChampions() { if (racers == null) { racers = new List<Racer>(); racers.Add(new Racer("張飛", "關羽", 100, "蜀國", 10, new string[] { "丈八蛇矛", "青龍偃月刀" }, new int[] { 200, 201, 202 })); racers.Add(new Racer("張飛", "關羽", 99, "蜀國", 10, new string[] { "丈八蛇矛", "青龍偃月刀" }, new int[] { 200, 201, 202 })); racers.Add(new Racer("黃忠", "魏延", 80, "蜀國", 10, new string[] { "穿楊弓", "大***" }, new int[] {203})); racers.Add(new Racer("許褚", "典韋", 95, "魏國", 10, new string[] { "大鐵錘", "雙戟" }, new int[] { 195, 212 })); racers.Add(new Racer("張遼", "徐晃", 90, "魏國", 10, new string[] { "長把子刀", "長把子斧" }, new int[] { 205, 106, 215 })); racers.Add(new Racer("程普", "黃蓋", 96, "吳國", 10, new string[] { "龍虎鞭", "大刀" }, new int[] { 190, 191, 202,207 })); racers.Add(new Racer("周泰", "太史慈", 88, "吳國", 10, new string[] { "無敵身軀", "火箭槍" }, new int[] { 195, 196, 197 })); } return racers; } public static IList<Team> GetConstructorChampions() { if (team == null) { team = new List<Team>(); team.Add(new Team("兄弟隊", new int[] { 200, 201, 202 })); team.Add(new Team("死黨隊", new int[] { 203 })); team.Add(new Team("虎營隊", new int[] { 195, 212 })); team.Add(new Team("良將隊", new int[] { 205, 106, 215 })); team.Add(new Team("老將隊", new int[] { 190, 191, 202, 207 })); team.Add(new Team("不死隊", new int[] { 195, 196, 197 })); } return team; } } }
===========================================Racer_IEqualityComparer.cs
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApplication4 { public class Racer_IEqualityComparer:IEqualityComparer<Racer> { //只比較Racer對象的FirstName屬性 public bool Equals(Racer x, Racer y) { return x.FirstName==y.FirstName; } public int GetHashCode(Racer obj) { return obj.FirstName.GetHashCode(); } } }
===========================================主程序
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Collections; using System.Collections.Concurrent; namespace ConsoleApplication4 { class Program { static void Main(string[] args) { //篩選(查詢贏了至少95場的吳國和蜀國軍隊軍隊)*************************************************************** //--Linq var query1 = from r in Formula.GetChampions() where r.Wins >= 95 && (r.Country == "吳國" || r.Country == "蜀國") select r; //--擴展方法 IEnumerable<Racer> ir1 = Formula.GetChampions(). Where(r => r.Wins >= 95 && (r.Country == "吳國" || r.Country == "蜀國")).Select(r => r); Foreach(query1); Foreach(ir1); //索引篩選(查詢贏了大于85場的、索引為偶數的軍團)************************************************************ //--擴展方法 IEnumerable<Racer> ir2 = Formula.GetChampions().Where((r, index) => r.Wins > 85 && index % 2 == 0).Select(r => r); Foreach(Formula.GetChampions()); Foreach(ir2); //類型篩選************************************************************************************************** //--擴展方法 object[] o = new object[] { 1, 2, "張飛", 2, "關二爺", true, 1.1 }; IEnumerable<string> is1 = o.OfType<string>();//篩選為string類型的元素 Foreach(is1); //復合from子句(查詢所有的武器)******************************************************************************* //--Linq var query2 = from r in Formula.GetChampions() from c in r.Arms select c; Foreach(query2); //--擴展方法 IEnumerable<string> is2 = Formula.GetChampions().SelectMany(r => r.Arms, (r, a) => new { Racer = r, Amrs = a }). Select(r => r.Racer.FirstName + "|" + r.Racer.LastName + "=>" + r.Amrs); Foreach(is2); //排序(先按照第一個姓名降序排序,在按照贏得次數升序排序,返回10個數據)**************************************** //--Linq var query3 = (from r in Formula.GetChampions() orderby r.FirstName descending, r.Wins ascending select r).Take(10); Foreach(query3); //--擴展方法 IEnumerable<Racer> is3 = Formula.GetChampions().OrderByDescending(r => r.FirstName).ThenBy(r => r.Wins).Select(r => r);//第一種方法(ThenBy) IEnumerable<Racer> is4 = Formula.GetChampions().OrderByDescending(r => r.FirstName). CreateOrderedEnumerable(r => r.Wins, Comparer<int>.Default, true).Select(r => r).Take(10);//第二種方法(CreateOrderedEnumerable) Foreach(is4); //分組(根據國家分組,并且只顯示軍團大于或等于2的國家)************************************************************ //--Linq var query4 = from r in Formula.GetChampions() group r by r.Country into g where g.Count() >= 2 select new { Count = g.Count(), Key = g.Key }; foreach (var item in query4) { Console.WriteLine("國家:{0}。軍團數:{1}", item.Key, item.Count); } //--擴展方法 var is5 = Formula.GetChampions().GroupBy(r => r.Country).Where(r => r.Count() >= 2).Select(r => new { Count = r.Count(), Key = r.Key }); foreach (var item in is5) { Console.WriteLine("國家:{0}。軍團數:{1}", item.Key, item.Count); } Console.WriteLine("================================================"); //對嵌套的對象分組(對國家進行分組,查詢出國家名稱,國家的軍團數,軍隊的將軍)****************************** //--Linq var query5 = from r in Formula.GetChampions() group r by r.Country into g select new { Key = g.Key, Count = g.Count(), Name = from n in g select new { LastName = n.LastName, FirstName = n.FirstName } }; foreach (var item in query5) { Console.WriteLine("國家:{0}。軍團數:{1}", item.Key, item.Count); foreach (var subitem in item.Name) { Console.WriteLine("軍隊的將軍:" + subitem.FirstName + "|" + subitem.LastName); } } Console.WriteLine("================================================"); //--擴展方法 var is6 = Formula.GetChampions().GroupBy(r => r.Country). Select(r => new { Country = r.Key, Count = r.Count(), Name = r.Select(n => new { FristName = n.FirstName, LastName = n.LastName }) }); foreach (var item in query5) { Console.WriteLine("國家:{0}。軍團數:{1}", item.Key, item.Count); foreach (var subitem in item.Name) { Console.WriteLine("軍隊的將軍:" + subitem.FirstName + "|" + subitem.LastName); } } Console.WriteLine("================================================"); //連接(查詢年份大于195的軍團和將軍)****************************************************************************** //--Linq 【語法: from r in 第一個對象 join t in 第二個對象 on r.one equals t.one select new { }】 int myyear = 190; var query7 = from racer in from r in Formula.GetChampions() from ry in r.Years where ry > myyear select new { Year = ry, Name = r.FirstName + "|" + r.LastName } join team in from t in Formula.GetConstructorChampions() from ty in t.Years where ty > myyear select new { Year = ty, Name = t.Name } on racer.Year equals team.Year select new { TeamName = team.Name, Year = team.Year, Name = racer.Name }; foreach (var item in query7) { Console.WriteLine("軍團:{0},將軍:{1},年份:{2}", item.TeamName, item.Name, item.Year); } Console.WriteLine("================================================"); //集合操作********************************************************************************************************************** //Racer_IEqualityComparer類實現了IEqualityComparer,只比較Racer對象的FirstName屬性 //委托,傳一個武器名稱,查詢屬于該武器的所有元素 Func<string, IEnumerable<Racer>> myracer = racer => from r in Formula.GetChampions() from a in r.Arms where a == racer select r; //----Distinct() 排除重復的數據 Foreach(Formula.GetChampions().Distinct(new Racer_IEqualityComparer())); //----Union() 并集 【(1,2,3,5)并集(5,6)=(1,2,3,5,6)】 Foreach(myracer("長把子刀").Union(myracer("大***"))); //----Intersect 交集 【(1,2,3,5)交集(5,6)=(5)】 Foreach(Formula.GetChampions().Intersect(myracer("大***"))); //----Except 差集 【(1,2,3,5)差集(5,6)=(1,2,3)】 Foreach(Formula.GetChampions().Except(myracer("大***"))); //合并************************************************************************************************************************* Foreach(myracer("長把子刀").Zip(myracer("大***"), (r, t) => r.FirstName + "|" + t.FirstName));//輸出:張遼|黃忠 //分區【Skip:跳過指定數量的元素】【Take:顯示的數量】************************************************************************* int pageindex = 1;//當前頁 int pagesize = 3;//每夜顯示多少數據 Foreach(Formula.GetChampions().Skip(pageindex * pagesize).Take(pagesize)); //聚合操作符(返回一個值)****************************************************************************************************** //----Count() 返回集合中的項數 Foreach(from r in Formula.GetChampions() select r.Years.Count()); //----Sum() 返回所有數字的和 Console.WriteLine((from r in Formula.GetChampions() select r.Years.Count()).Sum()); //----Min() 返回集合中最小的元素 Console.WriteLine((from r in Formula.GetChampions() select r.Years.Count()).Min()); //----Max() 返回集合中最小的元素 Console.WriteLine((from r in Formula.GetChampions() select r.Years.Count()).Max()); //----Average() 返回集合的平均值 Console.WriteLine((from r in Formula.GetChampions() select r.Years.Count()).Average()); //----Aggregate() 聚合運算 Console.WriteLine((from r in Formula.GetChampions() select r.Years.Count()).Aggregate((a, b) => a * b)); //轉換(根據將軍查詢武器)*********************************************************************************************** //查詢可以推遲到訪問數據項時再執行,在迭代中使用查詢時,查詢會執行。而使用轉換操作符會立即執行查詢,把查詢放在數組,列表或字典中 var query8 = (from r in Formula.GetChampions() select new { Name = r.FirstName, Racer = r }).ToList().ToLookup(r => r.Name, r => r.Racer); Foreach(query8["張飛"]); //在非類型化的集合上(如ArrayList)使用Linq查詢,就可以使用cast()方法 ArrayList abc= new ArrayList(); var aaaaa = (from ii in abc.Cast<Racer>() select ii); //操作符***************************************************************************************************************** //--填充一個范圍的數字 Foreach(Enumerable.Range(1, 20).Where(r => r > 10).Select(r => r)); //--返回一個空集合 List<string> l = Enumerable.Empty<string>().ToList(); //生成一個重復值的序列 Foreach(Enumerable.Repeat("asd", 3)); Console.ReadKey(); } static void Foreach<T>(IEnumerable<T> s) { foreach (T item in s) { Console.WriteLine(item); } //Parallel.ForEach(s, i => Console.WriteLine(i)); Console.WriteLine("========================="); } } }
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。