1024programmer Asp.Net How much do you know about the use of LINQ in C#? LINQ common function sorting, example source code analysis

How much do you know about the use of LINQ in C#? LINQ common function sorting, example source code analysis

How much do you know about the use of LINQ in C#? LINQ common function organization, example source code analysis

LINQ (Language-Integrated Query) is a powerful query technology in the C# language. It provides a unified query syntax that can be used to query and operate various data sources, including Collections, databases, XML, etc. The 20 function points of LINQ are described in detail below, and the corresponding source code is provided.

  1. Query all elements in the collection:
int[] numbers = { 1, 2, 3, 4, 5 };

 var query = from num in numbers
             select num;

 foreach (var num in query)
 {
     Console.WriteLine(num);
 }
  1. Use conditions to filter elements in a collection:
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. Sort the elements in the collection:
int[] numbers = { 5, 3, 1, 4, 2 };

 var query = from num in numbers
             orderbynum
             select num;

 foreach (var num in query)
 {
     Console.WriteLine(num);
 }
  1. Group elements in a collection:
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. Project the elements in the collection:
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. Connect elements of two collections:
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. Use LINQ to query the database:
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. Use LINQ to query XML documents:
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. Use LINQ to query JSON data:
string json = File.ReadAllText("data.json");
 var data = JsonConvert.DeserializeObject<List>(json);

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

 foreach (var name in query)
 {
     Console.WriteLine(name);
 }
  1. Use LINQ to query and convert the results:
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. Use LINQ query to return anonymous types:
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. Use LINQ query returns custom type:
public class Person
 {
     public string Name { get; set; }
     public int Age { get; set; }
 }

 List people = new List
 {
     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. Use LINQ query to return the collection:
int[] numbers = { 1, 2, 3, 4, 5 };

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

 List result = query.ToList();

 foreach (var num in result)
 {
     Console.WriteLine(num);
 }
  1. Use LINQ for paging query:
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. Use LINQ for aggregation operations:
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. Use LINQ to perform collection intersection operations:
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. Use LINQ to perform the union operation of sets:
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. Use LINQ to perform set difference operations:
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. Use LINQ to connect collections:
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. Use LINQ to reverse the order of collections:
int[] numbers = { 1, 2, 3, 4, 5 };

 var query = numbers.Reverse();

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

The above are sample codes for 20 commonly used LINQ function points, which demonstrate the powerful query capabilities and flexibility of LINQ in C#. By using LINQ, developers can more conveniently perform various data operations and queries, improving code readability and development efficiency.

This article is from the internet and does not represent1024programmerPosition, please indicate the source when reprinting:https://www.1024programmer.com/808568

author: admin

Previous article
Next article

Leave a Reply

Your email address will not be published. Required fields are marked *

Contact Us

Contact us

181-3619-1160

Online consultation: QQ交谈

E-mail: [email protected]

Working hours: Monday to Friday, 9:00-17:30, holidays off

Follow wechat
Scan wechat and follow us

Scan wechat and follow us

Follow Weibo
Back to top
首页
微信
电话
搜索