Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/kawser2133/linq-and-lambda-expressions
LINQ (Language-Integrated Query) and Lambda expressions are powerful features in C# that allow developers to query and manipulate data in a concise and expressive manner.
https://github.com/kawser2133/linq-and-lambda-expressions
Last synced: about 15 hours ago
JSON representation
LINQ (Language-Integrated Query) and Lambda expressions are powerful features in C# that allow developers to query and manipulate data in a concise and expressive manner.
- Host: GitHub
- URL: https://github.com/kawser2133/linq-and-lambda-expressions
- Owner: kawser2133
- Created: 2024-02-24T15:34:15.000Z (9 months ago)
- Default Branch: main
- Last Pushed: 2024-02-29T16:10:45.000Z (9 months ago)
- Last Synced: 2024-02-29T17:30:49.993Z (9 months ago)
- Homepage: https://binarybytez.com/understanding-linq-and-lambda-expressions
- Size: 11.7 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# LINQ and Lambda expressions in C#
LINQ (Language-Integrated Query) and Lambda expressions are powerful features in C# that allow developers to query and manipulate data concisely and expressively. While both are used for similar purposes, they have distinct syntaxes and use cases. Let's explain with examples and provide multiple scenarios to illustrate the usage of LINQ and Lambda expressions in C#.You can visit my blog post- [Understanding LINQ and Lambda Expressions in C#](https://binarybytez.com/understanding-linq-and-lambda-expressions/)
## 1. LINQ (Language-Integrated Query):
**LINQ** is a set of features in C# that enables developers to query data from different data sources using a SQL-like syntax directly within the C# language.**Syntax:** LINQ syntax consists of keywords such as from, where, select, group by, order by, etc., which resemble SQL syntax.
```csharp
List numbers = new List { 1, 2, 3, 4, 5 };
var evenNumbers = from num in numbers
where num % 2 == 0
select num;
```## 2. Lambda Expressions:
**Lambda expressions** are anonymous functions that allow developers to write inline delegate functions without explicitly defining a method.**Syntax:** Lambda expressions consist of the => (arrow) operator, parameters, and an expression or statement block.
```csharp
List numbers = new List { 1, 2, 3, 4, 5 };
var evenNumbers = numbers.Where(num => num % 2 == 0);
```## Examples with multiple scenarios
**1. Filtering a List of Objects:**
**Using LINQ:**
```csharp
List numbers = new List { 1, 2, 3, 4, 5 };
var evenNumbers = from num in numbers
where num % 2 == 0
select num;
```**Using Lambda Expression:**
```csharp
var evenNumbers = numbers.Where(num => num % 2 == 0);
```**2. Sorting a List of Objects:**
**Using LINQ:**
```csharp
List fruits = new List { "Apple", "Banana", "Orange", "Grape" };
var sortedFruits = from fruit in fruits
orderby fruit descending
select fruit;
```**Using Lambda Expression:**
```csharp
var sortedFruits = fruits.OrderByDescending(fruit => fruit);
```**3. Selecting Specific Properties from a List of Objects:**
**Using LINQ:**
```csharp
List people = new List
{
new Person { Name = "Alice", Age = 25 },
new Person { Name = "Bob", Age = 30 },
new Person { Name = "Charlie", Age = 28 }
};
var names = from person in people
select person.Name;
```**Using Lambda Expression:**
```csharp
var names = people.Select(person => person.Name);
```**4. Filtering and Projecting Data from a List of Objects:**
**Using LINQ:**
```csharp
List people = new List
{
new Person { Name = "Alice", Age = 25 },
new Person { Name = "Bob", Age = 30 },
new Person { Name = "Charlie", Age = 28 }
};
var adults = from person in people
where person.Age >= 18
select new { person.Name, person.Age };
```**Using Lambda Expression:**
```csharp
var adults = people.Where(person => person.Age >= 18)
.Select(person => new { person.Name, person.Age });
```**5. Grouping Data from a List of Objects:**
**Using LINQ:**
```csharp
List people = new List
{
new Person { Name = "Alice", Age = 25 },
new Person { Name = "Bob", Age = 30 },
new Person { Name = "Charlie", Age = 28 }
};
var ageGroups = from person in people
group person by person.Age < 30 into youngGroup
select new
{
IsYoung = youngGroup.Key,
People = youngGroup.ToList()
};
```**Using Lambda Expression:**
```csharp
var ageGroups = people.GroupBy(person => person.Age < 30)
.Select(youngGroup => new
{
IsYoung = youngGroup.Key,
People = youngGroup.ToList()
});
```These examples demonstrate various scenarios where LINQ and Lambda expressions are used to query, filter, project, sort, and group data in C#. By understanding and mastering these features, developers can write concise and expressive code for data manipulation tasks.
**Note:** The `Person` class used in these examples is assumed to have properties `Name` and `Age`.
Feel free to ask if you have any questions or need further clarification! [![linkedin](https://img.shields.io/badge/linkedin-0A66C2?style=for-the-badge&logo=linkedin&logoColor=white)](https://www.linkedin.com/in/kawser2133)