LINQ (Language Integrated Query) is a powerful and versatile feature of C# that allows developers to query data from various sources, such as databases, XML documents, and in-memory collections, in a type-safe and efficient manner. It provides a uniform syntax for querying data regardless of the data source and allows for the seamless integration of data queries with the rest of the C# programming language.
Using LINQ, developers can write concise and expressive code to filter, transform, and aggregate data, making it easier to manipulate data in complex applications. LINQ also supports deferred execution, which means that queries are not executed until the data is actually needed, resulting in improved performance and reduced memory usage.
Whether you're working on a small project or a large-scale enterprise application, LINQ can help you write more maintainable and readable code, reduce development time, and increase productivity.
Now let's see some example of Linq in C# :
Filtering
This is the most basic query and is used to filter data based on certain conditions. Here's an example:
var filteredList = myList.Where(item => item.Age > 18);
This will filter out all the items in the myList collection where the Age property is less than or equal to 18.
Projection
This query is used to project the data from one type to another type. Here's an example:
var projectedList = myList.Select(item => new { Name = item.FirstName + " " + item.LastName, Age = item.Age });
This will project the data in the myList collection into a new anonymous type with Name and Age properties.
Ordering
This query is used to order the data based on a specific property. Here's an example:
var orderedList = myList.OrderBy(item => item.LastName);
This will order the data in the myList collection based on the LastName property.
Grouping
This query is used to group the data based on a specific property. Here's an example:
var groupedData = myList.GroupBy(item => item.Department);
This will group the data in the myList collection based on the Department property.
Aggregation
This query is used to perform an aggregate operation on the data, such as calculating the sum or average of a property. Here's an example:
var sumOfAges = myList.Sum(item => item.Age);
This will calculate the sum of the Age property for all items in the myList collection.
Joining
This query is used to join two collections based on a common property. Here's an example:
var joinedData = myList.Join(myOtherList,
item => item.DepartmentId,
otherItem => otherItem.Id,
(item, otherItem) => new { Name = item.Name, DepartmentName = otherItem.Name });
This will join the myList collection with the myOtherList collection based on the DepartmentId and Id properties, and project the results into a new anonymous type with Name and DepartmentName properties.
Distinct
This query is used to remove duplicates from a collection. Here's an example:
var distinctData = myList.Distinct();
This will remove any duplicate items from the myList collection.
Any and All
These queries are used to check if any or all items in a collection satisfy a certain condition. Here are some examples:
var hasAdults = myList.Any(item => item.Age > 18);
var allAdults = myList.All(item => item.Age > 18);
The hasAdults variable will be true if any item in the myList collection has an Age greater than 18, and the allAdults variable will be true if all items in the myList collection have an Age greater than 18.
Take and Skip
These queries are used to take a certain number of items from the beginning or skip a certain number of items from the beginning of a collection. Here are some examples:
var firstThree = myList.Take(3);
var skipThree = myList.Skip(3);
The firstThree variable will contain the first three items in the myList collection, and the skipThree variable will contain all items in the myList collection except for the first three.
Top comments (0)