LINQ(数据查询)

如果只有空的构造函数,想要对字段进行初始化,可以直接在构造函数后面加上{属性名=值,属性名=值};属性与属性之间用,分割开

 //查询所有武学级别大于8的武林高手
            //var res = new List<MartialArtsMaster>();
            //foreach (var temp in masterList)
            //{
            //    if (temp.Level > 8)
            //    {
            //        res.Add(temp);
            //    }   
            //}
            //1,使用LINQ做查询( 表达式写法)
            //var res = from m in masterList //m是别名,是用来做限制的
            //          //from后面设置查询的集合
            //          where m.Level > 8 && m.Menpai=="丐帮" //通过&&添加并列的条件
            //          //where后面跟上查询的条件
            //          select m;//表示m的结果结合返回,表示你要返回的什么东西,这个是全部,还可以只返回名字
            //2,扩展方法的写法
            //var res = masterList.Where(Test1);
            //var res = masterList.Where(m => m.Level > 8 && m.Menpai=="丐帮" );


            //3,LINQ 联合查询
            //取得所学功夫的杀伤力大于90 的武林高手
            //var res = from m in masterList
            //    from k in kongfuList
            //    where m.Kongfu == k.Name && k.Power > 90
            //    //select new {master = m, kongfu = k};//创建一个临时对象,包括master 和 kongfu 两个字段
            //    select m;
            //扩展方法用法
            //var res =
            //    masterList.SelectMany(m => kongfuList, (m, k) => new {master = m, kongfu = k})
            //        .Where(x => x.master.Kongfu == x.kongfu.Name && x.kongfu.Power>90 );

            //4,对查询结果做排序 orderby (descending)
            //var res = from m in masterList
            //          //from后面设置查询的集合
            //          where m.Level > 8 && m.Menpai == "丐帮" //通过&&添加并列的条件
            //          //orderby m.Age descending
            //          orderby m.Level,m.Age //按照多个字段进行排序,如果字段的属性相同,就按照第二个属性排序
            //          //where后面跟上查询的条件
            //          select m;//表示m的结果结合返回

            //var res = masterList.Where(m => m.Level > 8 && m.Menpai == "丐帮").OrderBy(m => m.Age);
            //var res = masterList.Where(m => m.Level > 8).OrderBy(m => m.Level).ThenBy(m => m.Age);


            //5,join on 集合联合
            //var res = from m in masterList
            //    join k in kongfuList on m.Kongfu equals k.Name
            //    where k.Power>90
            //    select new {master = m, kongfu = k};

            //6,分组查询 into groups (把武林高手按照所学功夫分类,看一下那个功夫修炼的人数最多)
            //var res = from k in kongfuList
            //    join m in masterList on k.Name equals m.Kongfu
            //    into  groups
            //    orderby groups.Count()
            //    select new {kongfu = k, count = groups.Count() };
            

            //7,按照自身字段分组 group
            //var res = from m in masterList
            //    group m by m.Kongfu
            //    into g
            //    select new {count = g.Count(), key = g.Key};//g.Key Key表示是按照那个属性分的组

            //8,量词操作符 any all 判断集合中是否满足某个条件
            //bool res  = masterList.Any(m => m.Menpai == "长留");
            //Console.WriteLine(res);
            bool res = masterList.All(m => m.Menpai == "丐帮");
            Console.WriteLine(res);


            //foreach (var temp in res)
            //{
            //    Console.WriteLine(temp);
            //}
            Console.ReadKey();
        }

        //过滤方法
        static bool Test1(MartialArtsMaster master)
        {
            if (master.Level > 8) return true;
            return false;
        }

 

posted @ 2017-08-20 11:23  千喜ya  阅读(848)  评论(0编辑  收藏  举报