📅  最后修改于: 2023-12-03 15:32:39.407000             🧑  作者: Mango
LINQ (Language Integrated Query) is a feature of Microsoft .NET Framework that provides querying capabilities to .NET languages. LINQ lets you query data from different data sources like SQL Server, XML documents, and in-memory collections like List, Array, etc.
One of the most used LINQ operators is "Where". It allows filtering items in a collection based on a specific condition. In case of List, you can apply "Where" to filter items based on any property of the objects in the list.
Here is an example of using "Where" with a List of custom objects:
// Define a class
public class Person
{
public string Name { get; set; }
public int Age { get; set; }
}
// Create a List of Persons
var people = new List<Person>
{
new Person { Name = "John", Age = 30 },
new Person { Name = "Jane", Age = 25 },
new Person { Name = "Bob", Age = 40 },
new Person { Name = "Alice", Age = 22 }
};
// Apply "Where" to filter based on age
var adults = people.Where(p => p.Age >= 30);
// Iterate over filtered items
foreach (var adult in adults)
{
Console.WriteLine($"{adult.Name} is an adult.");
}
Output:
John is an adult.
Bob is an adult.
In the above example, we have defined a class called "Person" with two properties Name and Age. We have created a List of Person objects and then applied "Where" operator to filter based on age. Finally, we have used a foreach loop to iterate over the filtered items and print a message.
You can apply multiple "Where" operators to filter based on multiple conditions. Here is an example:
// Define a class
public class Product
{
public string Name { get; set; }
public decimal Price { get; set; }
public bool Available { get; set; }
}
// Create a List of Products
var products = new List<Product>
{
new Product { Name = "Product A", Price = 10.5M, Available = true },
new Product { Name = "Product B", Price = 20.0M, Available = false },
new Product { Name = "Product C", Price = 5.0M, Available = true },
new Product { Name = "Product D", Price = 15.0M, Available = true }
};
// Apply "Where" to filter based on multiple conditions
var availableProducts = products.Where(p => p.Available && p.Price <= 15);
// Iterate over filtered items
foreach (var product in availableProducts)
{
Console.WriteLine($"{product.Name} is available for {product.Price:C}.");
}
Output:
Product A is available for $10.50.
Product C is available for $5.00.
In the above example, we have defined a class called "Product" with three properties Name, Price and Available. We have created a List of Product objects and then applied "Where" operator to filter based on multiple conditions like the product is available and the price is less than or equal to 15. Finally, we have used a foreach loop to iterate over the filtered items and print a message.
In conclusion, "Where" is a powerful LINQ operator that allows filtering items in a List based on any property of the objects in the list. It can be combined with other operators to filter based on multiple conditions.