【LINQ 查询】

C#LINQ 查询【学习笔记】,

【LINQ 查询】    2016-10-24


事例:   先定义多个类Master和KongFu,然后在那五个类中定义变量

  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=陆,Name=”林平之”,Age=二3,Menpai=”仙堂山”, Kongfu=”追魂夺命剑”, Level=7},
    1一 newMaster(){Id=7,Name=”岳不群”, Age=50,Menpai=”五老峰”,Kongfu=”日月身法”,Level=八},
    1二 newMaster(){Id=八,Name=”令狐冲”,Age=二三,Menpai=”黄山”,Kongfu=”独孤玖剑”, Level=10},
    ①三 newMaster(){Id=九,Name=”梅超风”,Age=23,Menpai=”梅花岛”,Kongfu=”美女拳”, Level=十},
    1肆 newMaster(){Id=10,Name=”黄药士”,Age=二三,Menpai=”春梅岛”,Kongfu=”弹指神功”,Level=十},
    一伍 newMaster(){ Id = 1一, Name = “风清扬”, Age = 二3, Menpai = “华山”, Kongfu = “独孤9剑”, Level = 十 }
    1陆 };
    17 var kongfu =newList()
    1八 {
    1九 newKongFu(){Id=一,Name=”打狗棒法”, Power=九伍 },
    20 newKongFu(){Id=2,Name=”降龙10八掌”,Power=100},
    贰一 newKongFu(){Id=三,Name=”玄天指”, Power=拾0},
    2二 newKongFu(){Id=四,Name=”独孤玖剑”, Power=十0},
    贰三 newKongFu(){Id=5,Name=”玉蜂针”, Power=十0},
    二四 newKongFu(){Id=6,Name=”瞬神功”, Power=十0},
    二五 };
    二陆 }

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

 

始发树立查询:

【第一种】:找出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)

查询【学习笔记】, 【LINQ 查询】
2016-十-二四 事例: 先定义八个类Master和KongFu,然后在那三个类中定义变量 一class Master 2 { 3 publicintId{…

namespace LINQ01
【金沙注册送58】上学笔记,Linq联合查询。{
class Program
{
static void Main(string[] args)
{
//初步化学武器林好手
var masterList = new List<MartialArtsMaster>()
{
new MartialArtsMaster(){ Id = 一, Name = “黄蓉”, Age = 1八, Menpai =
“丐帮”, Kongfu = “打狗棒法”, Level = 玖 },
new MartialArtsMaster(){ Id = 贰, Name = “洪7公”, Age = 70, Menpai =
“丐帮”, Kongfu = “打狗棒法”, Level = 10 },
new MartialArtsMaster(){ Id = 三, Name = “王进泽”, Age = 22, Menpai =
“丐帮”, Kongfu = “降龙10八掌”,Level = 10 },
new MartialArtsMaster(){ Id = 4, Name = “任我行”, Age = 50, Menpai =
“明教”, Kongfu = “慕容剑法”, Level = 1 },
new MartialArtsMaster(){ Id = 5, Name = “东方不败”,Age = 35, Menpai =
“明教”, Kongfu = “柳絮剑法日月神教功夫日月心法”, Level = 十 },
new MartialArtsMaster(){ Id = 陆, Name = “林平之”, Age = 贰三, Menpai =
“莲峰山”, Kongfu = “葵花宝典”, Level = 7 },
new MartialArtsMaster(){ Id = 七, Name = “岳不群”, Age = 50, Menpai =
“敬亭山”, Kongfu = “参合指”, Level = 8 },
new MartialArtsMaster() { Id = 八, Name = “令狐冲”, Age = 二三, Menpai =
“阿尔山”, Kongfu = “独孤九剑”, Level = 10 },
new Martial阿特sMaster() { Id = 玖, Name = “梅超风”, Age = 2三, Menpai =
“桃花岛”, Kongfu = “美女剑法”, Level = 捌 },
new MartialArtsMaster() { Id =10, Name = “黄药士”, Age = 二3, Menpai =
“红绿梅岛”, Kongfu = “落英神剑掌”, Level = 拾 },
new Martial阿特sMaster() { Id = 1一, Name = “风清扬”, Age = 二三, Menpai =
“黄山”, Kongfu = “独孤玖剑”, Level = 10 }
};
//开始化学武器学
var kongfuList = new List<Kongfu>()
{
new Kongfu(){Id=1, Name=”打狗棒法”, Power=90},
new Kongfu(){Id=二, Name=”降龙十捌掌”,Power=玖五},
new Kongfu(){Id=3, Name=”无影金针”, Power=100},
new Kongfu(){Id=四, Name=”独孤玖剑”, Power = 100 },
new Kongfu(){Id=5, Name=”玉女素心剑法”, Power = 100 },
new Kongfu(){Id=陆, Name=”劈空掌”, Power = 十0 }
};
//方法壹:查询全体开学等第大于八的国手
//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 > 八 && 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};

事例:

//运用2:联合查询(扩张方法法)
//表达:m => kongfuList是 masterList.SelectMany另3个询问对象,
//(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);

 

//运用3表明式方法:将查询结果按规范排序(私下认可升序 ,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,然后在那七个类中定义变量

//运用3:扩屏方法
//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 res一=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=陆,Name=”林平之”,Age=2三,Menpai=”龙鹤山”, Kongfu=”柳絮剑法日月神教功夫日月心法”, Level=7},
    11 newMaster(){Id=7,Name=”岳不群”, Age=50,Menpai=”盘山”,Kongfu=”柳絮剑法日太阴星君教武功日月心法”,Level=八},
    1二 newMaster(){Id=八,Name=”令狐冲”,Age=二三,Menpai=”大茂山”,Kongfu=”独孤玖剑”, Level=十},
    一三 newMaster(){Id=九,Name=”梅超风”,Age=二三,Menpai=”梅花岛”,Kongfu=”空明拳”, Level=10},
    14 newMaster(){Id=十,Name=”黄药剂师”,Age=2三,Menpai=”红绿梅岛”,Kongfu=”瞬神功”,Level=10},
    一5 newMaster(){ Id = 11, Name = “风清扬”, Age = 贰3, Menpai = “岳麓山”, Kongfu = “独孤九剑”, Level = 十 }
    16 };
    一7 var kongfu =newList()
    1八 {
    1九 newKongFu(){Id=壹,Name=”打狗棒法”, Power=九伍 },
    20 newKongFu(){Id=2,Name=”降龙十捌掌”,Power=100},
    二1 newKongFu(){Id=三,Name=”葵花宝典”, Power=十0},
    2二 newKongFu(){Id=四,Name=”独孤九剑”, Power=拾0},
    23 newKongFu(){Id=5,Name=”玉女素心剑法”, Power=十0},
    二4 newKongFu(){Id=六,Name=”刹那神功”, Power=十0},
    二五 };
    2陆 }

//运用肆: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 };
//运用5:分组查询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)

//运用6:根据笔者字段分组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只要有1个满足条件,就回去Ture
bool resAll = masterList.All(m => m.Menpai ==
“丐帮”);//All全体记录的Menpai均要满意条件,才回到Ture
bool resAll1 = masterList.All(m =>
m.Level>=一);//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;
金沙注册送58,}
}
}

相关文章

网站地图xml地图