Linq简单的查询,扩展方法-创新互联

----------------------------------------------------Racer.cs

成都创新互联坚持“要么做到,要么别承诺”的工作理念,服务领域包括:成都做网站、网站建设、企业官网、英文网站、手机端网站、网站推广等服务,满足客户于互联网时代的海曙网站设计、移动媒体设计的需求,帮助企业找到有效的互联网解决方案。努力成为您成熟可靠的网络建设合作伙伴!
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,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 Arms = null, IEnumerable years = null)
        {
            this.FirstName = firstname;
            this.LastName = lasename;
            this.Wins = wins;
            this.Country = country;
            this.Starts = starts;
            List LArms = new List();
            foreach (var item in Arms)
            {
                LArms.Add(item);
            }
            this.Arms = LArms.ToArray();
            List Lyears = new List();
            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 ConsoleApplication1
{
    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 ConsoleApplication1
{
    public static class Formula
    {
        private static List racers;
        private static List team;
        public static IList GetChampions()
        {
            
            if (racers == null)
            {
                racers = new List();
                racers.Add(new Racer("张飞", "关羽", 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 GetConstructorChampions()
        {
            if (team == null)
            {
                team = new List();
                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;
        }
       

    }
}

----------------------------------------------------主程序

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();

            
            //===============================================扩展方法
            List RacersCondition = new List(Formula.GetChampions());
            //Where:扩展方法,用于筛选
            //OrderBy:扩展方法,用于排序
            //Select:扩展方法,将值投放到新表中
            IEnumerable RacersResult = RacersCondition.Where(r => r.Country == "吴国").OrderBy(r => r.Wins).Select(r => r);
            foreach (var item in RacersResult)
            {
                Console.WriteLine(item);
            }
            Console.ReadKey();
        }
    }
}

创新互联www.cdcxhl.cn,专业提供香港、美国云服务器,动态BGP最优骨干路由自动选择,持续稳定高效的网络助力业务部署。公司持有工信部办法的idc、isp许可证, 机房独有T级流量清洗系统配攻击溯源,准确进行流量调度,确保服务器高可用性。佳节活动现已开启,新人活动云服务器买多久送多久。


文章标题:Linq简单的查询,扩展方法-创新互联
标题路径:http://lszwz.com/article/ddpedh.html

其他资讯

售后响应及时

7×24小时客服热线

数据备份

更安全、更高效、更稳定

价格公道精准

项目经理精准报价不弄虚作假

合作无风险

重合同讲信誉,无效全额退款