SoFunction
Updated on 2025-03-07

Implementation of LINQ to Objects query in C#

LINQ to Objects is an application of LINQ technology in C#. It is specially used to query and operate on object collections in memory. By using LINQ to Objects, you can use a unified syntax to query, filter, sort, group and operate various .NET objects. This article will introduce the basic concepts, common operations and examples of LINQ to Objects in detail to help you better understand how to use LINQ to Objects for querying and processing object collections in C#.

1. Basic concepts of LINQ to Objects

LINQ to Objects is part of the LINQ technology, which enables you to query and manipulate .NET objects in memory. These objects can be any type provided by the .NET Framework, such as collections, arrays, lists, etc. LINQ to Objects simplifies data processing and operation by providing a unified query syntax, separating the query process from the actual implementation of the underlying data source.

In LINQ to Objects, you can use query expressions or method syntax to write queries to perform various operations on collections of objects, such as filtering, sorting, grouping, etc. The query of LINQ to Objects can be targeted at any implementationIEnumerable<T>The data source of the interface.

2. Common LINQ to Objects operations

Here are some common LINQ to Objects operations and examples:

2.1 Query operation

usefromKeywords specify the data source, usewhereFilter keywords, useselectKeyword projection:

var result = from person in people
             where  > 18
             select ;

2.2 Method syntax

Use method chaining to call standard query operators, such asWhereSelectOrderBywait:

var result = (person =>  > 18)
                   .Select(person => );

2.3 Sort

useOrderByorOrderByDescendingSort ascending or descending:

var sortedPeople = (person => );

2.4 Grouping

useGroupByGroup according to the specified attributes:

var groupedPeople = (person => );

2.5 Aggregation

useSumAverageCountData aggregation:

var totalAge = (person => );
var averageAge = (person => );
var personCount = ();

3. Example of LINQ to Objects

Here is an example of using LINQ to Objects to operate on a collection of people:

using System;
using ;
using ;
class Person
{
    public string Name { get; set; }
    public int Age { get; set; }
    public string Department { get; set; }
}
class Program
{
    static void Main(string[] args)
    {
        List<Person> people = new List<Person>
        {
            new Person { Name = "Alice", Age = 25, Department = "HR" },
            new Person { Name = "Bob", Age = 30, Department = "IT" },
            new Person { Name = "Carol", Age = 22, Department = "IT" },
            new Person { Name = "David", Age = 28, Department = "HR" }
        };
        var itEmployees = from person in people
                          where  == "IT"
                          select ;
        var averageAge = (person => );
        var groupedPeople = from person in people
                            group person by ;
        ("IT Employees:");
        foreach (var employeeName in itEmployees)
        {
            (employeeName);
        }
        ("Average age: " + averageAge);
        ("Grouped People:");
        foreach (var group in groupedPeople)
        {
            ($"{}: {()} people");
        }
    }
}

In the above example, we use LINQ to Objects to perform multiple operations on the collection of people, including filtering, grouping, and aggregation. With LINQ to Objects, we are able to handle collections of objects in memory in a more concise way.

4. Summary

LINQ to Objects is a powerful tool in C# that enables you to query and manipulate collections of .NET objects in a unified syntax. By using query expressions or method syntax, you can easily perform data filtering, sorting, grouping, aggregation and more in your code. With LINQ to Objects, you can write more readable and maintainable code, which improves development efficiency and code quality. Whether it is processing collection data or querying objects in memory, mastering LINQ to Objects will make you more comfortable in C# development.

This is the end of this article about the implementation of LINQ to Objects query in C#. For more related C# LINQ to Objects query content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!