asp.net mvc框架之Linq

查询数据的时候有两种写法:

第一种:使用lambda的方式写的,官方没有正式的叫法,我们就叫“lambda”写法,

第二种:使用Linq写法,是微软发明的一种类似SQL的语法,给我们一个新选择。两种方法是可以相互替代的,没有哪个好,哪个坏,看个人习惯。需要join等复杂用法的时候Linq更易懂,一般的时候“lambda”写法更清晰,更紧凑。

通过反编译得知,这两种写法最终编译成同样的东西,所以本质上一样的。

class Program
    {
        static void Main(string[] args)
        {
            Person p1 = new Person() { Id = 1, Name = "人1", Salary = 2000, Age = 22 };
            Person p2 = new Person() { Id = 2, Name = "人2", Salary = 3000, Age = 23 };
            Person p3 = new Person() { Id = 3, Name = "人3", Salary = 4000, Age = 24 };
            Person p4 = new Person() { Id = 4, Name = "人4", Salary = 2200, Age = 21 };
            Person p5 = new Person() { Id = 5, Name = "人5", Salary = 2500, Age = 30 };

            List<Person> list_persons = new List<Person>()
            {
                p1,p2,p3,p4,p5
            };


            #region 查询一个字段的数据,类似:select name from 表名
            var selectName = from p in list_persons
                             select p.Name;
            foreach (var name in selectName)
            {
                Console.WriteLine(name);
            }
            #endregion

            #region 查询多个字段的数据,类似:select 字段1,字段2,... from 表名

            var manyField = from p in list_persons
                            select new { FieldId = p.Id, FieldName = p.Name };
            foreach (var item in manyField)
            {
                Console.WriteLine(item);
            }

            #endregion

            #region 排序
            var orderbyfields = from p in list_persons
                                orderby p.age, p.salary descending, p.id descending
                                select p;
            foreach (var item in orderbyfields)
            {
                console.writeline(item.name);
            }
            #endregion

            #region 联合查询

            Master m1 = new Master() { Id = 1, Name = "主人1" };
            Master m2 = new Master() { Id = 2, Name = "主人2" };
            Master m3 = new Master() { Id = 3, Name = "主人3" };

            List<Master> list_masters = new List<Master>() { m1, m2, m3 };


            Dog d1 = new Dog() { Id = 1, Name = "田园犬", MasterId = 3 };
            Dog d2 = new Dog() { Id = 2, Name = "泰迪", MasterId = 2 };
            Dog d3 = new Dog() { Id = 3, Name = "二哈", MasterId = 2 };
            Dog d4 = new Dog() { Id = 4, Name = "斗犬", MasterId = 1 };
            Dog d5 = new Dog() { Id = 5, Name = "土狗", MasterId = 1 };
            List<Dog> list_dogs = new List<Dog>() { d1, d2, d3, d4, d5 };

            //在join中相等不要用"==",而是使用equals
            var join_dogs_master = from d in list_dogs
                                   join m in list_masters on d.MasterId equals m.Id
                                   select new { DogName = d.Name, MasterName = m.Name };
            foreach (var item in join_dogs_master)
            {
                Console.WriteLine(item);
            }


            #endregion

            #region 分组

            var OrderbySalary = from p in list_persons
                                group p by p.Salary into g
                                select new { Key = g.Key, MaxAge = g.Max(p => p.Age) };
            foreach (var item in OrderbySalary)
            {
                Console.WriteLine(item);
            }

                                #endregion

            Console.ReadKey();

        }
    }

    class Master
    {
        public int Id { get; set; }
        public string Name { get; set; }
    }

    class Dog
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public int MasterId { get; set; }
    }

    class Person
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public int Salary { get; set; }
        public int Age { get; set; }
    }

    class Teacher
    {
        public Teacher()
        {
            this.stus = new List<Student>();
        }

        public int Id { get; set; }
        public string Name { get; set; }
        public int Age { get; set; }
        public List<Student> stus { get; set; }

    }

    class Student
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public int Age { get; set; }
    }

只有Where,Select,OrderBy,GroupBy,Join等这些能用Linq写法,如果要使用"Max,Min,Count,Average,Sum,Any,First,FirstOrDefault,Single,SingleOrDefault,Distinct,Skip"等则还是要用lambda的写法

posted @ 2019-07-04 01:21  black娃  阅读(1104)  评论(0编辑  收藏  举报