C# linq基本用法及语句介绍

本文介绍了C# LINQ的基本用法,包括from、join、let、where、orderby和group等语句。通过示例代码展示了如何在C#中进行数据查询和操作,帮助理解LINQ的强大功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

C# LINQ

LINQ允许我们以使用SQL查询数据库的方式来查询数据集合。 下面我们查看一段代码,代码演示了如何使用LINQ的示例。

        static void Main(string[] args)
        {
            int[] numbers = { 1, 2, 3, 4,5,6,7,8 };         //数据源
            IEnumerable<int> lowNums =                //定义并存储查询
                               from n in numbers
                               where n < 4
                               select n;

            foreach (var x in lowNums)                //执行查询
            Console.Write("{0}, ", x);
        }

代码产生了如下输出:

1, 2, 3,

语句介绍

  • from
  • join
  • let
  • where
  • orderby
  • group

from

from子句是最基本的,基本语法如下: from 变量 in 集合

  int[] arr = { 1, 2, 3 };
            var query = from n in arr
                        where n < 2 
                        select n;
                        
    foreach (var item in query)
                Console.WriteLine(item);

输出结果

1

join

  • 连接多个集合的数据
  • 连接
        public class A
        {
            public string name;
            public string id;
        }

        public class B
        {
            public string sex;
            public string id;
        }

        static A[] aS = new A[]
        {
            new A{ name = "小明",id = "01"},
            new A{ name = "小白",id = "02"},
            new A{ name = "小黑",id = "03"},
        };


        static B[] bS = new B[]
        {
            new B{ sex = "男",id = "01"},
            new B{ sex = "男",id = "02"},
            new B{ sex = "女",id = "03"},
        };


        static void Main(string[] args)
        {
            var quert = from a in aS
                        join b in bS on a.id equals b.id
                        where b.sex == "男"
                        select a.name;

            foreach (var item in quert)
            {
                Console.WriteLine("性别为男的姓名:" + item);
            }
            Console.ReadKey();
        }

输出结果:

性别为男的姓名:小明
性别为男的姓名:小白

let

static void Main(){
   var groupA = new[] { 3, 4, 5, 6 };
   var groupB = new[] { 6, 7, 8, 9 };

   var someInts = from a in groupA
                  from b in groupB
                  let sum = a + b        ←在新的变量中保存结果
                  where sum == 12
                  select new {a, b, sum};

   foreach (var a in someInts)
      Console.WriteLine(a);}

输出结果

{ a = 3, b = 9, sum = 12 }
{ a = 4, b = 8, sum = 12 }
{ a = 5, b = 7, sum = 12 }
{ a = 6, b = 6, sum = 12 }

where

static void Main(){
   var groupA = new[] { 3, 4, 5, 6 };
   var groupB = new[] { 6, 7, 8, 9 };

   var someInts = from int a in groupA
                  from int b in groupB
                  let sum = a + b
                  where sum >= 11          ←条件1
                  where a == 4             ←条件2
                  select new {a, b, sum};

   foreach (var a in someInts)
      Console.WriteLine(a);}

输出结果

{ a = 4, b = 7, sum = 11 }
{ a = 4, b = 8, sum = 12 }
{ a = 4, b = 9, sum = 13 }

orderby

static void Main( ) {
   var students = new []       //匿名类型的对象数组
   {
      new { LName="Jones",   FName="Mary",   Age=19, Major="History" },
      new { LName="Smith",   FName="Bob",    Age=20, Major="CompSci" },
      new { LName="Fleming", FName="Carol",  Age=21, Major="History" }
   };

   var query = from student in students
               orderby student.Age      ←根据Age排序
               select student;

   foreach (var s in query) {
      Console.WriteLine("{0}, {1}:  {2} - {3}",
               s.LName, s.FName, s.Age, s.Major);
   }}

输出结果:

Jones, Mary:  19 – HistorySmith, 
Bob:  20 – CompSciFleming,
Carol:  21 – History

group

static void Main( ){
   var students = new[]       //匿名类型的对象数组
   {
      new { LName="Jones",   FName="Mary",  Age=19, Major="History" },
      new { LName="Smith",   FName="Bob",   Age=20, Major="CompSci" },
      new { LName="Fleming", FName="Carol", Age=21, Major="History" }
   };

   var query = from student in students
               group student by student.Major;

   foreach (var s in query)            //枚举分组
   {
      Console.WriteLine("{0}", s.Key);
                               --↑--
                               分组键
      foreach (var t in s)             //枚举分组中的项
         Console.WriteLine("      {0}, {1}", t.LName, t.FName);
   }}

输出结果:

History
      Jones, Mary
      Fleming, 
CarolCompSci
      Smith, Bob
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值