USING LINQ IN C#
LINQ (Language Integrated Query) is a set of language features that provides a unified syntax for querying data from various sources. It allows you to write queries in a syntax that is similar to SQL, but is integrated into the C# or VB.NET language.
Here are a few examples of LINQ queries in C#:
- Querying a list of integers to find all numbers that are greater than 5:
List<int> numbers = new List<int> { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
var result = from n in numbers
where n > 5
select n;
- Querying a list of strings to find all strings that start with the letter "A":
List<string> words = new List<string> { "apple", "banana", "cherry", "date", "elderberry" }; var result = from w in words
where w.StartsWith("A")
select w;
- Querying an array of objects to find all objects that have a certain property value:
class Person { public string FirstName { get; set; } public string LastName { get; set; } public int Age { get; set; } } Person[] people = {
new Person { FirstName = "John", LastName = "Doe", Age = 30 },
new Person { FirstName = "Jane", LastName = "Doe", Age = 28 },
new Person { FirstName = "Bob", LastName = "Smith", Age = 35 },
new Person { FirstName = "Alice", LastName = "Smith", Age = 32 }
}; var result = from p in people
where p.LastName == "Smith"
select p;
Comments
Post a Comment