当前位置: 澳门新濠3559 > 编程 > 正文

然后在这两个类中定义变量

时间:2019-11-09 19:39来源:编程
【LINQ 查询】 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks;   namespace LINQ01 { class Program { static void Main(string[] args) { //初始化武林高手 var master

【LINQ 查询】

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

 

namespace LINQ01
{
class Program
{
static void Main(string[] args)
{
//初始化武林高手
var masterList = new List<MartialArtsMaster>()
{
new MartialArtsMaster(){ Id = 1, Name = "黄蓉", Age = 18, Menpai = "丐帮", Kongfu = "打狗棒法", Level = 9 },
new MartialArtsMaster(){ Id = 2, Name = "洪七公", Age = 70, Menpai = "丐帮", Kongfu = "打狗棒法", Level = 10 },
new MartialArtsMaster(){ Id = 3, Name = "郭靖", Age = 22, Menpai = "丐帮", Kongfu = "降龙十八掌",Level = 10 },
new MartialArtsMaster(){ Id = 4, Name = "任我行", Age = 50, Menpai = "明教", Kongfu = "葵花宝典", Level = 1 },
new MartialArtsMaster(){ Id = 5, Name = "东方不败",Age = 35, Menpai = "明教", Kongfu = "葵花宝典", Level = 10 },
new MartialArtsMaster(){ Id = 6, Name = "林平之", Age = 23, Menpai = "华山", Kongfu = "葵花宝典", Level = 7 },
new MartialArtsMaster(){ Id = 7, Name = "岳不群", Age = 50, Menpai = "华山", Kongfu = "葵花宝典", Level = 8 },
new MartialArtsMaster() { Id = 8, Name = "令狐冲", Age = 23, Menpai = "华山", Kongfu = "独孤九剑", Level = 10 },
new MartialArtsMaster() { Id = 9, Name = "梅超风", Age = 23, Menpai = "桃花岛", Kongfu = "九阴真经", Level = 8 },
new MartialArtsMaster() { Id =10, Name = "黄药师", Age = 23, Menpai = "梅花岛", Kongfu = "弹指神通", Level = 10 },
new MartialArtsMaster() { Id = 11, Name = "风清扬", Age = 23, Menpai = "华山", Kongfu = "独孤九剑", Level = 10 }
};
//初始化武学
var kongfuList = new List<Kongfu>()
{
new Kongfu(){Id=1, Name="打狗棒法", Power=90},
new Kongfu(){Id=2, Name="降龙十八掌",Power=95},
new Kongfu(){Id=3, Name="葵花宝典", Power=100},
new Kongfu(){Id=4, Name="独孤九剑", Power = 100 },
new Kongfu(){Id=5, Name="九阴真经", Power = 100 },
new Kongfu(){Id=6, Name="弹指神通", Power = 100 }
};
//方法一:查询所有开学级别大于8的高手
//var res = new List<MartialArtsMaster>();
//foreach (var temp in masterList)
//{
// if (temp.Level > 8)
// {
// res.Add(temp);
// }
//}
//方法二:使用linq查寻(表达式与法)
//var res = from m in masterList//from后面设置查询的集合
// where m.Level > 8 && m.Menpai == "丐帮"//条件语句可以用&&或||
// select m.Name;//把m的集合返回

 2016-10-24

//方法三:扩展方法的写法.where(过滤方法),意思就是这个方法会遍列自身的所有成员并当作参数分别传入过滤方法,在过滤方法中进行所要的判断。
//var res = masterList.Where(Test1);//
//方法四:一般直接使用lamda表达式
//var res = masterList.Where(m=>m.Level>8 && m.Menpai== "丐帮");


//运用一:联合查询(表达式法)
//查询Powe>90的Naster
//var res = from m in masterList
// from k in kongfuList
// where m.Kongfu==k.Name && k.Power > 90
// select new { master=m,kongfu=k};

事例:

//运用二:联合查询(扩展方法法)
//说明:m => kongfuList是 masterList.SelectMany另一个查询对象,
//(m, k) => new { master = m, Kongfu = k }是将两个欲查询的对象用lamda表达式建立一个新的new { master = m, Kongfu = k }集合,这里边的就是两个查询对象的合集(from m in masterList from k in kongfuList)
// Where(x=>x.master.Kongfu==x.Kongfu.Name);就是条例语句
//var res = masterList.SelectMany(m => kongfuList, (m, k) => new { master = m, Kongfu = k });
//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);

 

//运用三表达式方法:将查询结果按条件排序(默认升序 ,orderby XXX descending 按XXX降序,XXX可以是多个用,分隔的条件字段 )
//var res = from r in masterList
// where r.Age>20
// orderby r.Level,r.Age descending
// select r;

先定义两个类Master和KongFu,然后在这两个类中定义变量

//运用三:扩屏方法
//var res = masterList.SelectMany(m => kongfuList,(m,k)=>new { master=m,kongfu=k}).Where(x=>x.master.Age>30).OrderBy(o=>o.master.Level);
//var res1=masterList.Where(m => m.Age > 30).OrderBy(k => k.Level).ThenBy(n=>n.Age);//多个条件ThenBY(条件一相同时,按条件二排)

  1. 1 class Master

    2 {
    3     publicintId{ get;set;}
    4     publicstringName{ get;set;}
    5     publicintAge{ get;set;}
    6     publicstringKongfu{ get;set;}
    7     publicstringMenPai{ get;set;}
    8     publicintLevel{ get;set;}
    9     public overide string ToString()
    

    10 { 11 return string.Fromat("Id:{0},Name:{1},Age:{2},Kongfu:{3},MenPai:{4},Level:{5}", Id, Name, Age, Kongfu, Menpai, Leve) 12 } 13 }

    1 class KongFu
    2 {
    3    public int Id { get;set;}
    4    public string Name { get;set;}
    5    public int Power { get;set;}
    6 
    7    public override string ToString()
    8    {
    9       return string.Format("Id:{0},Name:{1},Power:{2}", Id, Name, Power);
    

    10 } 11 }

     

    1 static void Main(String[] args)
    2 {
    3     var master =newList<Master>()
    4     {
    5         newMaster(){Id=1,Name="黄蓉",Age=18,Menpai="丐帮", Kongfu="打狗棒法",Level=9},
    6         newMaster(){Id=2,Name="洪七公",Age=70,Menpai="丐帮", Kongfu="打狗棒法",Level=10},
    7         newMaster(){Id=3,Name="郭靖",Age=22,Menpai="丐帮", Kongfu="降龙十八掌",Level=10},
    8         newMaster(){Id=4,Name="任我行",Age=50,Menpai="明教", Kongfu="葵花宝典",Level=1},
    9         newMaster(){Id=5,Name="东方不败",Age=35,Menpai="明教", Kongfu="葵花宝典", Level=10},
    

    10 newMaster(){Id=6,Name="林平之",Age=23,Menpai="华山", Kongfu="葵花宝典", Level=7}, 11 newMaster(){Id=7,Name="岳不群", Age=50,Menpai="华山",Kongfu="葵花宝典",Level=8}, 12 newMaster(){Id=8,Name="令狐冲",Age=23,Menpai="华山",Kongfu="独孤九剑", Level=10}, 13 newMaster(){Id=9,Name="梅超风",Age=23,Menpai="梅花岛",Kongfu="九阴真经", Level=10}, 14 newMaster(){Id=10,Name="黄药师",Age=23,Menpai="梅花岛",Kongfu="弹指神功",Level=10}, 15 newMaster(){ Id = 11, Name = "风清扬", Age = 23, Menpai = "华山", Kongfu = "独孤九剑", Level = 10 } 16 }; 17 var kongfu =newList() 18 { 19 newKongFu(){Id=1,Name="打狗棒法", Power=95 }, 20 newKongFu(){Id=2,Name="降龙十八掌",Power=100}, 21 newKongFu(){Id=3,Name="葵花宝典", Power=100}, 22 newKongFu(){Id=4,Name="独孤九剑", Power=100}, 23 newKongFu(){Id=5,Name="九阴真经", Power=100}, 24 newKongFu(){Id=6,Name="弹指神功", Power=100}, 25 }; 26 }

//运用四:join on联合查询
//var res = from m in masterList
// join k in kongfuList on m.Kongfu equals k.Name //此处不能用==,必须用equals
// where k.Power>90
// select new { master = m, konfu = k };
//运用五:分组查询into groups(把武林高手按照所学功夫分类,看一下那个功夫修炼得最多)
//var res = from k in kongfuList
// join m in masterList on k.Name equals m.Kongfu
// into groups//如果满足join on条件时,就into groups,
// orderby groups.Count()
// select new { kongfu = k,count = groups.Count() };//最将满足条件的k与count输出

开始建立查询:

【第一种】:找出Level大于8的列表来
         var result = new List<Master>();
            foreach (var item in master)
            {
                if (item.Level > 8)
                {
                    result.Add(item);
                }
            }
            foreach (var item in result)
            {
                Console.WriteLine(item.ToString());//ToString()不写默认存在
            }
            Console.ReadKey();
          //需要在 master类和KongFu中创建 ToString()方法,因为在查询完以后进行遍历(foreach)的时候h会出现以下错误

【第二种】:利用专业查询语言查找并排序
    //from:表示从哪个集合里查找……where:查新条件……select:返回结果列表
            var result2 = from m in master
                          where m.Level < 10//查询条件
                          orderby m.Level//根据level进行升序排序
                          orderby m.Level descending//根据level进行降序排序
                          select m;
            foreach (var item in result2)
            {
                Console.WriteLine(item);
            }
            Console.ReadKey();

  【按条件查找并排序】

            var ressult22 = from m in master.Where(m => m.Level < 10).OrderBy(m => m.Age)//OrderBy && OrderByDescending   升序和降序 排列
                            select m;
            foreach (var item in ressult22)
            {
                Console.WriteLine(item);
            }
            Console.ReadKey();


【第三种】:调用函数的方法来实现查找功能
        var result3 = master.Where(T1);
            foreach (var item in result3)
            {
                Console.WriteLine(item);
            }
            Console.ReadKey();
       //需要调用的类
        static bool T1(Master m)
        {
            if (m.Level < 10)
            { return true; }
            else
            { return false; }
        }


【第四种】:利用lamada表达式查找
        var result4 = master.Where(m => m.Level < 10);//lamada表达式    参数名=>表达式
            foreach (var item in result4)
            {
                Console.WriteLine(item);
            }
            Console.ReadKey();

【第五种】:将两个列表联合起来,形成 Count(master) * Count(Kongfu) 的列表
        var result5 = from m in master
                          from f in kongfu
                          select new { mm = m, ff = f };//
            foreach (var item in result5)
            {
                Console.WriteLine(item);
            }
            Console.ReadKey()

【第六种】:查询当 master.Level == Kongfu.Id,时两个的信息
        var result6 = from m in master
                          from f in kongfu
                          //where m.Level == f.Id
                          where m.Kongfu == f.Name && f.Power > 90
                          select new { mm = m, ff = f };
            foreach (var item in result6)
            {
                Console.WriteLine(item);
            }
            Console.ReadKey();

【第七种】:实现master和Kongfu中属性相等的信息输出
        var result7 = from m in master
                          //将kongfu列表中的信息放入k中,选出 m.Kongfu == k.Name的信息 
                          join k in kongfu on m.Kongfu equals k.Name
                          select new { mm = m, kk = k };
            foreach (var item in result7)
            {
                Console.WriteLine(item);
            }
            Console.ReadKey();

【第八种】:一人修炼一门功夫,一门功夫被多少人修炼
        var result8 = from k in kongfu
                          join m in master on k.Name equals m.Kongfu
                          into groups//把武林高手分类,看看哪门功夫被人修炼的多
                          orderby groups.Count()//按照修炼人进行
                          select new { kongfu = k, count = groups.Count() };//groups.count:获取到
            foreach (var item in result8)
            {
                Console.WriteLine(item);
            }
            Console.ReadKey();


       //将修炼功夫一样的人放在一起并输出master和kongfu的信息
            var result = from k in kongfu
                         join m in master on k.Name equals m.Kongfu
                         select new { mm = m, kk = k };
            foreach (var item in result)
            {
                Console.WriteLine(item);
            }
            Console.ReadKey();

【第九种】:按照自身帮派进行分组,
        var result9 = from m in master
                          group m by m.Menpai//将门派分组,放到m中
                              into p//表示放到p中
                              select new { count = p.Count(), key = p.Key };//count = p.Count()表示这组有多少人
            //key = p.Key表示这个是按照什么分的,即关键字"Key"为Menpai
            foreach (var item in result9)
            {
                Console.WriteLine(item);
            }
            Console.ReadKey();



【第十种】:确定序列中元素是否满足条件

       Any在内部循环遍历每个元素

       根据m=>m.Menpai判断是否满足条件
            bool result10 = master.Any(m => m.Menpai == "丐帮");
            Console.WriteLine(result10);
            Console.ReadKey();

 

 

来自为知笔记(Wiz)

//运用六:按照自身字段分组group by
var res = from m in masterList
group m by m.Kongfu into g
orderby g.Count()
select new { count = g.Count(), g.Key };//g.Key表示是按照那个属性分的组
foreach (var temp in res)
{
Console.WriteLine(temp.ToString());
}
Console.WriteLine(res.Count());

 

//量词操作符
bool resAny = masterList.Any(m => m.Menpai == "丐帮");//Any只要有一个满足条件,就返回Ture
bool resAll = masterList.All(m => m.Menpai == "丐帮");//All所有记录的Menpai均要满足条件,才返回Ture
bool resAll1 = masterList.All(m => m.Level>=1);//All所有记录的Menpai均要满足条件,才返回Ture
Console.WriteLine(resAny);
Console.WriteLine(resAll);
Console.WriteLine(resAll1);

Console.ReadKey();
}
//过滤方法
static bool Test1(MartialArtsMaster master)
{
if (master.Level > 8 && master.Menpai == "丐帮") return true;
return false;
}
}
}

编辑:编程 本文来源:然后在这两个类中定义变量

关键词: