c#-Linq-条件和排序

代码

using System;
using System.Collections.Generic;
using System.Linq;

namespace Linq
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");
            var query = from r in Formulal.GetChampions() where r.id<3 orderby r.id descending select r;
            foreach(A i in query)
            {
                Console.WriteLine(i.id);
            }
            Console.ReadLine();
        }
    }
    static class Formulal
    {
        private static List<A> s_racers;
        private static List<A> InitializeRacers() => new List<A> { new A(1,"1"), new A(2, "2"), new A(3, "3"), new A(4, "4") };
        public static IList<A> GetChampions() => s_racers ?? (s_racers = InitializeRacers());
    }
    class A
    {
        public A(int id,string name)
        {
            this.id = id;
            this.name = name;
        }
        public int id;
        public string name;
    }
}

运行结果

Hello World!
2
1
发布了476 篇原创文章 · 获赞 38 · 访问量 6万+

猜你喜欢

转载自blog.csdn.net/xie__jin__cheng/article/details/104051315