C#之Linq语句

Linq是一种查询语句,C#中可以用这种语句做集合和数据库查询。

新建一个Student类:

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

namespace ConsoleApplication1
{
    class Student
    {
        public int id;
        public string name;
        public int age;
        public int score;

        public Student (int id, string name, int age, int score)
        {
            this.id = id;
            this.name = name;
            this.age = age;
            this.score = score;
        }

        public override string ToString()
        {
            return string.Format("name: {0}, age: {1}, score: {2}", name, age, score);
        }
    }
}

Title类:

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

namespace ConsoleApplication1
{
    class Title
    {
        public int score;
        public string title;

        public Title (int score, string title)
        {
            this.score = score;
            this.title = title;
        }
    }
}

实例化集合,做查询用:

using System;
using System.Collections.Generic;
using System.Linq;

namespace ConsoleApplication1
{
    public class Program
    {
        public static void Main(string[] args)
        {
            List<Student> class1 = new List<Student>()
            {
                new Student(0, "一班小明", 5, 100),
                new Student(1, "一班小红", 6, 100),
                new Student(2, "一班小李", 5, 80),
                new Student(3, "一班小王", 7, 90)
            };

            List<Title> titles = new List<Title>()
            {
                new Title (100, "三好学生")
            };
        }
    }
}

注意,先引入System.Linq命名空间。

1.基本使用:

var result = from item in arr

                   where condition

                   select item;

from item in arr: 用来确定查询对象,arr是要查询的集合对象, item是arr中的对象。

where condition: where后接条件,筛选要返回的对象。

select item: select后接要返回的内容。

例:查询一班中分数为100的学生:

var students = from stu in class1
               where stu.score == 100
               select stu;

foreach (var item in students)
{
    Console.WriteLine(item);
}

打印结果:

select可以自由选择需要的数据返回,可以组合新的数据结构返回,比如,我们只需要查询分数为100的学生的name和age:

var students = from stu in class1
               where stu.score == 100
               select new
               {
                   stu.name,
                   stu.age
               };

foreach (var item in students)
{
    Console.WriteLine(item);
}

结果默认以变量名为键:

我们可以自定义键:

var students = from stu in class1
               where stu.score == 100
               select new
               {
                   姓名 = stu.name,
                   年龄 = stu.age
               };

foreach (var item in students)
{
    Console.WriteLine(item);
}

结果:

2.join ... in ... on ...:

join in on 用来连接另一个查询对象,on后接连接条件,例:查询分数为100的学生和其对应的称号:

var students = from stu in class1
               join title in titles on stu.score equals title.score
               where stu.score == 100
               select new { stu, title.title };

foreach (var item in students)
{
    Console.WriteLine(item);
}

打印结果:

3.group by:

group by用来对数据进行分组,例:通过年龄对一班学生进行分组:

var students = from stu in class1
               group stu by stu.age;

foreach (var item in students)
{
    Console.WriteLine(item.Key );
    foreach (var stu in item)
    {
        Console.WriteLine(stu);
    }
}

打印结果:

查询数据会以分组条件作为键,对应数据组成的列表作为值。

4.orderby:使用orderby对数据进行排序,可选参数:ascending(从小到大)、descending(从大到小),例:

查询一班学生按照分数从高到低排序:

var students = from stu in class1
               orderby stu.score descending
               select stu;

foreach (var item in students)
{
    Console.WriteLine(item);
}

 

  • 0
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值