----------------------------------------------------Racer.cs
成都創(chuàng)新互聯(lián)堅(jiān)持“要么做到,要么別承諾”的工作理念,服務(wù)領(lǐng)域包括:成都做網(wǎng)站、網(wǎng)站制作、企業(yè)官網(wǎng)、英文網(wǎng)站、手機(jī)端網(wǎng)站、網(wǎng)站推廣等服務(wù),滿足客戶于互聯(lián)網(wǎng)時(shí)代的奎文網(wǎng)站設(shè)計(jì)、移動媒體設(shè)計(jì)的需求,幫助企業(yè)找到有效的互聯(lián)網(wǎng)解決方案。努力成為您成熟可靠的網(wǎng)絡(luò)建設(shè)合作伙伴!
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Collections; namespace ConsoleApplication1 { [Serializable] public class Racer:IComparable<Racer>,IFormattable { public string FirstName { get; private set; }//第一個(gè)武將 public string LastName { get; private set; }//第二個(gè)武將 public int Wins { get; private set; }//贏得次數(shù) 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("第一個(gè)賽手:{0},最后一個(gè)賽手:{1},贏的次數(shù):{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 ConsoleApplication1 { public class Team { public string Name { get; private set; }//團(tuán)隊(duì)名稱 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 ConsoleApplication1 { 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("張飛", "關(guān)羽", 100, "蜀國", 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("兄弟隊(duì)", new int[] { 200, 201, 202 })); team.Add(new Team("死黨隊(duì)", new int[] { 203 })); team.Add(new Team("虎營隊(duì)", new int[] { 195, 212 })); team.Add(new Team("良將隊(duì)", new int[] { 205, 106, 215 })); team.Add(new Team("老將隊(duì)", new int[] { 190, 191, 202, 207 })); team.Add(new Team("不死隊(duì)", new int[] { 195, 196, 197 })); } return team; } } }
----------------------------------------------------主程序
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { var query = from r in Formula.GetChampions() where r.Country == "蜀國" orderby r.Wins descending select r; foreach (var item in query) { Console.WriteLine("{0}",item); } Console.ReadKey(); //===============================================擴(kuò)展方法 List<Racer> RacersCondition = new List<Racer>(Formula.GetChampions()); //Where:擴(kuò)展方法,用于篩選 //OrderBy:擴(kuò)展方法,用于排序 //Select:擴(kuò)展方法,將值投放到新表中 IEnumerable<Racer> RacersResult = RacersCondition.Where(r => r.Country == "吳國").OrderBy(r => r.Wins).Select(r => r); foreach (var item in RacersResult) { Console.WriteLine(item); } Console.ReadKey(); } } }
網(wǎng)站名稱:Linq簡單的查詢,擴(kuò)展方法
標(biāo)題網(wǎng)址:http://aaarwkj.com/article38/iggcsp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供自適應(yīng)網(wǎng)站、微信小程序、域名注冊、外貿(mào)建站、做網(wǎng)站、用戶體驗(yàn)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)