Linq使用1

 
using System;
using System.Collections.Generic;
using System.Text;
using System.Linq;
namespace Linq
{
    class Program
    {
        static void Main(string[] args)
        {
            /*
             Linq是.NET 3.5后提供的查询集合对象的技术,使用了类似sql的语法来实现查询集合
             */
            List list_st = new List();
            for (int i = 0; i < 100; i++)
            {
                Student st = new Student();
                st.Name = "Tom" + i.ToString();
                st.Age = i;
                list_st.Add(st);
            }

            //采用Linq关键字的查询表达式(自动映射相关方法)
            //var query = from r in list_st where r.Age > 50 orderby r.Age ascending select r;

            //采用Linq方法的查询表达式
            var query2 = list_st.Where(r => r.Age > 50).OrderBy(r => r.Age > 50).Select(r=>r);

            foreach (Student s in query2)
            {
                Console.WriteLine("姓名:{0}--年龄:{1}\r\n", s.Name, s.Age);
            }
            Console.WriteLine("使用Linq查询数据");
            Console.ReadKey();
        }
    }

    //学生数据
    struct Student
    {
        public string Name { get; set; }
        public int Age { get; set; }
    }
}

你可能感兴趣的:(C#,linq,query,list,string,struct,class)