C#中LINQ的使用知多少?LINQ常功能整理,源代码解析

开发 后端
以下是20个常用的LINQ功能点的示例代码,它们展示了LINQ在C#中的强大查询能力和灵活性。通过使用LINQ,开发人员可以更加方便地进行各种数据操作和查询,提高代码的可读性和开发效率。

LINQ(Language-Integrated Query)是C#语言中的一个强大的查询技术,它提供了一种统一的查询语法,可以用于查询和操作各种数据源,包括集合、数据库、XML等。下面详细描述了LINQ的20个功能点,并提供了相应的源代码。

  1. 查询集合中的所有元素:
int[] numbers = { 1, 2, 3, 4, 5 };

var query = from num in numbers
            select num;

foreach (var num in query)
{
    Console.WriteLine(num);
}
  1. 使用条件过滤集合中的元素:
int[] numbers = { 1, 2, 3, 4, 5 };

var query = from num in numbers
            where num % 2 == 0
            select num;

foreach (var num in query)
{
    Console.WriteLine(num);
}
  1. 对集合中的元素进行排序:
int[] numbers = { 5, 3, 1, 4, 2 };

var query = from num in numbers
            orderby num
            select num;

foreach (var num in query)
{
    Console.WriteLine(num);
}
  1. 对集合中的元素进行分组:
string[] names = { "Alice", "Bob", "Charlie", "David", "Eve" };

var query = from name in names
            group name by name[0];

foreach (var group in query)
{
    Console.WriteLine(group.Key);
    foreach (var name in group)
    {
        Console.WriteLine(name);
    }
}
  1. 对集合中的元素进行投影:
string[] names = { "Alice", "Bob", "Charlie", "David", "Eve" };

var query = from name in names
            select name.Length;

foreach (var length in query)
{
    Console.WriteLine(length);
}
  1. 连接两个集合的元素:
string[] names1 = { "Alice", "Bob", "Charlie" };
string[] names2 = { "David", "Eve", "Frank" };

var query = from name1 in names1
            join name2 in names2 on name1[0] equals name2[0]
            select new { Name1 = name1, Name2 = name2 };

foreach (var pair in query)
{
    Console.WriteLine($"{pair.Name1}, {pair.Name2}");
}
  1. 使用LINQ查询数据库:
using (var dbContext = new MyDbContext())
{
    var query = from user in dbContext.Users
                where user.Age > 18
                select user;

    foreach (var user in query)
    {
        Console.WriteLine(user.Name);
    }
}
  1. 使用LINQ查询XML文档:
XDocument doc = XDocument.Load("data.xml");

var query = from element in doc.Descendants("book")
            where (int)element.Element("price") > 10
            select element.Element("title").Value;

foreach (var title in query)
{
    Console.WriteLine(title);
}
  1. 使用LINQ查询JSON数据:
string json = File.ReadAllText("data.json");
var data = JsonConvert.DeserializeObject<List<Person>>(json);

var query = from person in data
            where person.Age > 18
            select person.Name;

foreach (var name in query)
{
    Console.WriteLine(name);
}
  1. 使用LINQ查询并转换结果:
int[] numbers = { 1, 2, 3, 4, 5 };

var query = from num in numbers
            where num % 2 == 0
            select num * 2;

foreach (var num in query)
{
    Console.WriteLine(num);
}
  1. 使用LINQ查询返回匿名类型:
string[] names = { "Alice", "Bob", "Charlie", "David", "Eve" };

var query = from name in names
            select new { Name = name, Length = name.Length };

foreach (var item in query)
{
    Console.WriteLine($"{item.Name}, {item.Length}");
}
  1. 使用LINQ查询返回自定义类型:
public class Person
{
    public string Name { get; set; }
    public int Age { get; set; }
}

List<Person> people = new List<Person>
{
    new Person { Name = "Alice", Age = 20 },
    new Person { Name = "Bob", Age = 25 },
    new Person { Name = "Charlie", Age = 30 }
};

var query = from person in people
            where person.Age > 25
            select person.Name;

foreach (var name in query)
{
    Console.WriteLine(name);
}
  1. 使用LINQ查询返回集合:
int[] numbers = { 1, 2, 3, 4, 5 };

var query = from num in numbers
            where num % 2 == 0
            select num * 2;

List<int> result = query.ToList();

foreach (var num in result)
{
    Console.WriteLine(num);
}
  1. 使用LINQ进行分页查询:
int pageNumber = 2;
int pageSize = 10;

var query = from product in dbContext.Products
            orderby product.Name
            select product;

var page = query.Skip((pageNumber - 1) * pageSize).Take(pageSize);

foreach (var product in page)
{
    Console.WriteLine(product.Name);
}
  1. 使用LINQ进行聚合操作:
int[] numbers = { 1, 2, 3, 4, 5 };

int sum = numbers.Sum();
int max = numbers.Max();
int min = numbers.Min();
double average = numbers.Average();

Console.WriteLine($"Sum: {sum}");
Console.WriteLine($"Max: {max}");
Console.WriteLine($"Min: {min}");
Console.WriteLine($"Average: {average}");
  1. 使用LINQ进行集合的交集操作:
int[] numbers1 = { 1, 2, 3, 4, 5 };
int[] numbers2 = { 4, 5, 6, 7, 8 };

var query = numbers1.Intersect(numbers2);

foreach (var num in query)
{
    Console.WriteLine(num);
}
  1. 使用LINQ进行集合的并集操作:
int[] numbers1 = { 1, 2, 3, 4, 5 };
int[] numbers2 = { 4, 5, 6, 7, 8 };

var query = numbers1.Union(numbers2);

foreach (var num in query)
{
    Console.WriteLine(num);
}
  1. 使用LINQ进行集合的差集操作:
int[] numbers1 = { 1, 2, 3, 4, 5 };
int[] numbers2 = { 4, 5, 6, 7, 8 };

var query = numbers1.Except(numbers2);

foreach (var num in query)
{
    Console.WriteLine(num);
}
  1. 使用LINQ进行集合的连接操作:
var query = from order in dbContext.Orders
            join customer in dbContext.Customers on order.CustomerId equals customer.Id
            select new { OrderId = order.Id, CustomerName = customer.Name };

foreach (var result in query)
{
    Console.WriteLine($"{result.OrderId}, {result.CustomerName}");
}
  1. 使用LINQ进行集合的逆序操作:
int[] numbers = { 1, 2, 3, 4, 5 };

var query = numbers.Reverse();

foreach (var num in query)
{
    Console.WriteLine(num);
}

以上是20个常用的LINQ功能点的示例代码,它们展示了LINQ在C#中的强大查询能力和灵活性。通过使用LINQ,开发人员可以更加方便地进行各种数据操作和查询,提高代码的可读性和开发效率。

责任编辑:姜华 来源: 今日头条
相关推荐

2024-09-18 05:20:00

C#代码

2009-09-08 10:35:24

LINQ技术

2009-09-14 16:12:57

LINQ删除记录

2009-09-10 14:54:15

LINQ使用Skip操

2009-09-08 16:08:44

Linq使用order

2009-09-09 16:21:31

Linq使用sqlme

2009-09-10 15:45:07

Linq使用Selec

2009-09-17 13:54:26

LINQ to XML

2013-08-27 10:20:35

JavaLINQLinq4j

2009-09-17 13:30:32

LINQ to XML

2009-09-10 16:32:19

LINQ Where

2009-09-09 16:01:21

Linq实体继承使用

2009-09-08 09:24:50

LINQ查询

2009-09-08 15:39:13

Linq使用Inser

2012-05-10 11:25:34

LINQ

2009-08-27 09:13:28

LINQ查询表达式

2024-06-20 13:38:03

2009-01-04 16:58:48

LINQ to XMLLINQXML

2009-09-08 16:02:47

Linq使用Group

2009-09-09 15:37:27

Linq DataLo
点赞
收藏

51CTO技术栈公众号