C#编程 LINQ查询

LINQ查询表达式

 

约束

LINQ查询表达式必须以from子句开头,以select或group子句结束

关键字

from...in...:指定要查找的数据以及范围变量,多个from子句则表示从多个数据源查找数据。注意:C#编译器会把"复合from子句"的查询表达式转换为SelectMany()扩展方法

join...in...on...equals...:指定多个数据源的关联方式

let:引入用于存储查询表达式中子表达式结果的范围变量,通常能达到层次感会更好,使代码更易于月的

orderby、descending:指定元素的排序字段和排序方式,当有多个排序字段时,由字段顺序确定主次关系,可指定升序和降序两种排序方式

where:指定元素的筛选条件,多个where子句则表示了并列条件,必须全部都满足才能入选,每个where子句可以使用&&、||连接多个条件表达式

group:指定元素的分组字段

select:指定查询要返回的目标数据,可以指定任何类型,甚至是匿名类型(目前通常被指定为匿名类型)

into:提供一个临时的标识符,该标识符可以引用join、group和select子句的结果。(1)直接出现在join子句之后的into关键字会被翻译为GroupJoin。(2)select或group子句字后的into它会重新开始一个查询,让我们可以继续引入where、orderby和select子句,它是对分步构建查询表达式的一种简写方式。

 

下面通过一个案例来学习对两张表进行查询

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

namespace _013_LINQ {
    /// <summary>
    /// 武林高手
    /// </summary>
    class MartialArtsMaster {
        public int Id { get; set; }
        public string Name { get; set; }
        public int Age { get; set; }
        public string Menpai { get; set; }
        public string Kongfu { get; set; }
        public int Level { get; set; }

        public override string ToString()
        {
            return string.Format("Id: {0}, Name: {1}, Age: {2}, Menpai: {3}, Kongfu: {4}, Level: {5}", Id, Name, Age, Menpai, Kongfu, Level);
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _013_LINQ {
    /// <summary>
    /// 武学
    /// </summary>
    class Kongfu {
        public int Id { get; set; }
        public string Name { get; set; }
        public int Power { get; set; }

        public override string ToString()
        {
            return string.Format("Id: {0}, Name: {1}, Power: {2}", Id, Name, Power);
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;

namespace _013_LINQ
{
    internal class Program
    {
        private 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 && temp.Menpai == "丐帮")
    {
    res.Add(temp);
}

1 使用LINQ查询,表达式写法

            //1,使用LINQ做查询( 表达式写法)
            var res = from m in masterList
                      //from后面设置查询的集合
                      where m.Level > 8 && m.Menpai == "丐帮"
                      //where后面跟上查询的条件
                      select m;//表示m的结果结合返回

使用LINQ查询,扩展方法的写法。其中Where是过滤操作符,根据返回bool值的Func委托参数过滤元素

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

        //2,扩展方法的写法
        //var res = masterList.Where(Test1);
        var res = masterList.Where(m => m.Level > 8 && m.Menpai == "丐帮");

查询结果:

 

LINQ也可以同时查多个表,下面我们取得所学功夫的杀伤力大于90 的武林高手。

2 LINQ 联合查询

            var res = from m in masterList
                      from k in kongfuList
                      where m.Kongfu == k.Name && k.Power > 90
                      select m;

LINQ 联合查询,扩展方法用法

            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 (descending) 

优先等级排序,然后年龄排序

            var res = from m in masterList
                      where m.Level > 8 && m.Menpai == "丐帮" 
                      //orderby m.Age descending  // 默认从小到大,加上descending从大到小
                      orderby m.Level, m.Age //按照多个字段进行排序,如果字段的属性相同,就按照第二个属性排序
                      select m;//表示m的结果结合返回

排序扩展用法

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

如果排序判断条件有多个,后面的排序要用ThenBy

var res = masterList.Where(m => m.Level > 8).OrderBy(m => m.Level).ThenBy(m => m.Age);

查询结果:

 

4 Join on集合联合查询

            var res = from m in masterList
                      // join...in... 表示要连接的表,on后面为连接条件,等于要用equals,不能用==
                      join k in kongfuList on m.Kongfu equals k.Name
                      where k.Power > 90
                      select new { master = m, kongfu = k };

查询结果:

 

 5 分组查询 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() };

查询结果:

 

6 按照自身字段分组 group by 

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

结果:

 

7 量词操作符 any all   判断集合中是否满足某个条件 

any判断集合中是否有一个满足,all判断集合中是否全部满足

            bool res = masterList.Any(m => m.Menpai == "长留");
            Console.WriteLine(res);
            res = masterList.All(m => m.Menpai == "丐帮");
            Console.WriteLine(res);

结果:

 

posted @ 2018-12-10 00:36  穆玄琅  阅读(26702)  评论(0编辑  收藏  举报