Ejemplo n.º 1
0
        // GET: ExampeLINQambda
        public ActionResult Index()
        {
            ExampleGenriceList genriceList = new ExampleGenriceList();
            var list = genriceList.GetAll();
            //当前我们对list这个集合进行LINQ简单查询
            var linqListl = from x in list
                            select x;
            //SQL条件精确查询:select*from表名 where条件
            var linqList2 = from x in list
                            where x.Name == "前四"//精确查询
                            select x;
            //SQL条件模糊查询:select*from表名 字段 like '%软件%'
            var linqList3 = from x in list
                            where x.ClassName.Contains("软件")//Contains:包含
                            select x;
            //SQL排序:select*from list order by Name desc
            var linqList4 = from x in list
                            orderby x.Name descending// descending; 降序排序
                            select x;

            //SQl计数:select count(*) from list
            var linqList5 = (from x in list
                             select x).Count();

            //SQL联接:select*from x inner join on y where x.Id=y.Id
            var linqList6 = from x in list
                            join y in list on x.Id equals y.Id
                            select x;

            //SQL分组查询:select count(*) from list group by ClassName
            var linqList7 = from x in list
                            group x by x.ClassName into y
                            select new
            {
                y.Key,
                count = y.Count()
            };

            //SQL取第一条数据:select top 1 * from list
            var linqList8 = (from x in list
                             select x).FirstOrDefault();

            //SQL取第10条数据:select top 1 * from list
            var linqList9 = (from x in list
                             select x)
                            .Take(10);
            //取从16条数据~第30条数据
            var linqList10 = (from x in list
                              select x)
                             .Skip(15).Take(30);

            return(View());
        }
        public ActionResult Index()
        {
            ExampleGenriceList genriceList = new ExampleGenriceList();
            var stuList = genriceList.GetAll();
            //模糊查询
            IEnumerable <StudentDemo> list1 = stuList.Where(x => x.ClassName.Contains("软件"));
            //精确查询
            IEnumerable <StudentDemo> list2 = stuList.Where(k => k.Name == "王五");
            //获取符合条件的单条数据
            StudentDemo stu1 = stuList.Where(k => k.Name == "王五").FirstOrDefault();


            return(null);
        }