一段关于linq的代码

来源:互联网 发布:app开放端口什么意思 编辑:程序博客网 时间:2024/05/17 00:54
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;


namespace linq2
{
    class Program
    {
        class cl
        {
            public string id { get; set; }
            public string a { get; set; }
            public string b { get; set; }
            public string c { get; set; }
            public string d { get; set; }
            public override string ToString()
            {
                return "id: " + id + " a: " + a + " b: " + b + " c: " + c + " d: " + d;
            }
        }
        static void Main(string[] args)
        {
            List<cl>cls=new List<cl>{
                new cl{id="a",a="1",b="one",c="aa",d="d"},
                new cl{id="b",a="2",b="two",c="bb",d="d"},
                new cl{id="c",a="3",b="three",c="cc",d="d3"},
                new cl{id="d",a="4",b="four",c="dd",d="d"},
                new cl{id="e",a="5",b="five",c="ee",d="d5"},
                new cl{id="f",a="6",b="six",c="ff",d="d"},
                new cl{id="g",a="7",b="seven",c="gg",d="d7"},
                new cl{id="h",a="8",b="eight",c="hh",d="d"}};
            var qu=from c in cls where c.d=="d"select c;
            foreach (cl cll in qu)
            {
                Console.WriteLine(cll);
            }
            Console.WriteLine();
            var q = from cc in cls orderby cc.b select cc;
            foreach (cl i in q)
            {
                Console.WriteLine(i);
            }
            Console.WriteLine();
            var it=cls.OrderBy(c=>c.a).ThenBy(c=>c.b).ThenBy(c=>c.c).ThenBy(c=>c.d).Select(c=>c);
            foreach(cl ite in it)
            {
                Console.WriteLine(ite);
            }
            Console.WriteLine();
            var i1 = from c in cls group c by c.b into bg select bg;
            foreach (var i2 in i1)
            {
                Console.WriteLine(i2);
            }
            bool anyd = cls.Any(c => c.d == "d");
            bool alld = cls.All(c => c.d == "d");
            Console.WriteLine("{0} {1}", anyd, alld);
            Console.ReadKey();
        }
    }
}
0 0
原创粉丝点击