C# LINQ

LINQ定义

LINQ(Language Integrated Query)是 C# 中用于查询和操作数据的强大工具。它提供了一种统一的方式不仅限于查询数据库,还可以用来查询任何数据源,包括 XML 文档、ADO.NET 数据集、对象集合等。

LINQ常用部分和使用方式

LINQ主要分为以下部分:

1.LINQ to Objects:直接查询在内存中的对象集合,如数组,列表,对象等

2.LINQ to SQL:使用 LINQ 查询 SQL Server 数据库。

3.LINQ to DataTable:使用 LINQ 查询 ADO.NET 数据集。

4.LINQ to XML:处理 XML 数据的查询。

5.LINQ to Entities:用于实体框架,非常适合用于使用数据库时的对象关系映射

有两种查询方式

1.方法语法:数据源.方法(条件)

2.查询语法:类似于sql语法,语句格式:from 迭代变量  in 数据源  where 条件 select 迭代变量

1.Linq To Object  

 //查询大于0的整数
 int[] numInt =  {-1,1,6,-2,4};
 //查询语法
 var res = from x in  numInt where x>0 select x;
 //方法语法
 var res2 = numInt.Where(x=>x>0);


 //查询小于0的最大整数
 List<int> list = new List<int>(numInt);
 //查询语法
 var max = (from x in list where x < 0 select x).Max();//有些查询操作没有等效的查询表达式,只能结合方法查询如最大最小值,平均值等等。

 //方法语法
 var max2 = list.Where(x => x < 0).Max(); 

 //根据学生名字相加年龄
 List<Student> students = new List<Student>();
 students.Add(new Student("甲", 12));
 students.Add(new Student("甲", 13));
 students.Add(new Student("乙", 10));
 students.Add(new Student("乙", 11));
 students.Add(new Student("丙", 15));

 var sumInt = from x in students group x by x.Name into g select new { name = g.Key, Sum = g.Sum(x => x.Age) };

 var sumInt2 = students
     .GroupBy(x => x.Name)
     .Select(g => new { Key = g.Key, Sum = g.Sum(x => x.Age) });


 List<Student> students2 = new List<Student>();
 students2.Add(new Student("丙", 55));

 //join语句
 var joinKey = from x in students join y in students2 on x.Name equals y.Name select new { x, y };

2.LinqTo SQL

using System;
using System.Linq;
using System.Data.Linq;

class Program
{
    static void Main()
    {
        DataContext db = new DataContext("your_connection_string");

        // 查询数据库中的 Customers 表
        var customers = from c in db.GetTable<Customer>()
                        where c.City == "London"
                        select c;

        foreach (var customer in customers)
        {
            Console.WriteLine(customer.Name);
        }
    }
}

3.LINQ to Table


DataTable dt = new DataTable();
dt.Columns.Add("Name", typeof(string));
dt.Columns.Add("age", typeof(int));
foreach (var student in students)
{
   dt.Rows.Add(student.Name, student.Age);
}
//筛选年龄大于10
dt.AsEnumerable().Where(x => x.Field<int>("age") > 10);
//或者
dt.Select("age>10");

4.Linq  To XML

using System;
using System.Linq;
using System.Xml.Linq;

class Program
{
    static void Main()
    {
        XElement xml = XElement.Load("data.xml");

        // 查询 XML 中的元素
        var items = from item in xml.Elements("Item")
                    where (int)item.Element("Price") > 100
                    select item;

        foreach (var item in items)
        {
            Console.WriteLine(item.Element("Name").Value);
        }
    }
}

5.Linq  to Entities (Entity Framework)

using System;
using System.Linq;

class Program
{
    static void Main()
    {
        using (var context = new YourDbContext())
        {
            // 查询数据库中的 Customers 表
            var customers = context.Customers.Where(c => c.City == "London");

            foreach (var customer in customers)
            {
                Console.WriteLine(customer.Name);
            }
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值