LINQ學習重點(WHERE)
Restriction operators: The where keyword
The where keyword or Where method provide this capability. These operators restrict, or filter, the input sequence to produce an output sequence.
- Your first query: the structure of a LINQ query.
- Filter elements on a property: filter elements based on a single property
- Filter elements using multiple properties: test multiple properties to filter elements in a sequence.
- Filter and drilldown into the output elements: filter input elements, then drill into a sequence on each output element.
- Filter input elements based on index: use an element's position to filter it.
//Filter elements based on position(這種寫法,第1次看)
static void Ex4()
{
string[] digits = { "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine" };
var shortDigits = digits.Where((digit, index) => digit.Length < index); // "five", "six", "seven", "eight", "nine" , 這幾個被篩選出來
WriteLine("==Query Result==");
foreach (var item in shortDigits)
{
WriteLine($" The Word <{item}> 比它的索引值短");
}
}
//Examine a sequence property of output elements
static void Ex3()
{
var customers = GetCustomerList();
var tpeCustomers = from cus in customers
where cus.Region == "TPE"
select cus;
WriteLine("==Query Result==");
foreach (var item in tpeCustomers)
{
WriteLine($" Customer<{item.CustomerID}> : {item.CompanyName}");
foreach (var order in item.Orders)
{
WriteLine($" OrderID <{order.OrderID}> : {order.OrderDate : yyyy/MM/dd}");
}
}
}
//Filter elements on multiple properties
static void Ex2()
{
List<Product> products = GetProductList();
var expensiveProducts = from prod in products
where prod.UnitsInStock > 0 && prod.UnitPrice > 3.00M
select prod;
WriteLine("==Query Result==");
foreach (var item in expensiveProducts)
{
WriteLine($"{item.ProductName} 在庫:({item.UnitsInStock}) & 金額大於 3.00M:({item.UnitPrice})");
}
}
//Filter elements on a property
static void Ex1()
{
List<Product> products = GetProductList();
var soldOuts = from prod in products
where prod.UnitsInStock == 0
select prod;
WriteLine("Sold out Products : ");
foreach (var item in soldOuts)
{
WriteLine($"{ item.ProductName } is sold Out.");
}
}
static List<Product> GetProductList()
{
var list = new List<Product>();
list.Add(new Product { ProductName = "Cheese", UnitPrice = 3000.58M, UnitsInStock = 20 });
list.Add(new Product { ProductName = "Apple", UnitPrice = 5M, UnitsInStock = 10 });
list.Add(new Product { ProductName = "Banana", UnitPrice = 2.55M, UnitsInStock = 0 });
list.Add(new Product { ProductName = "Milk", UnitPrice = 258M, UnitsInStock = 365 });
list.Add(new Product { ProductName = "Cherry", UnitPrice = 147M, UnitsInStock = 15 });
return list;
}
static List<Customer> GetCustomerList()
{
var list = new List<Customer>();
list.Add(new Customer()
{
CustomerID = "Kevin" ,
CompanyName ="BOT" ,
Region ="TPE" ,
Orders = new List<Order> { new Order { OrderID="1" , OrderDate = Convert.ToDateTime("2020/11/26") }
, new Order { OrderID="2" , OrderDate = Convert.ToDateTime("2020/11/27") } }
});
list.Add(new Customer()
{
CustomerID = "Bob",
CompanyName = "LAND",
Region = "TPE",
Orders = new List<Order> { new Order { OrderID="6" , OrderDate = Convert.ToDateTime("2020/10/01") }
, new Order { OrderID="7" , OrderDate = Convert.ToDateTime("2020/10/03") } }
});
list.Add(new Customer()
{
CustomerID = "Judy",
CompanyName = "TFMI",
Region = "KAO",
Orders = new List<Order> { new Order { OrderID="3" , OrderDate = Convert.ToDateTime("2020/11/03") }
, new Order { OrderID="4" , OrderDate = Convert.ToDateTime("2020/11/15") } }
});
return list;
}
}
class Product
{
public string ProductName { get; set; }
public int UnitsInStock { get; set; }
public decimal UnitPrice { get; set; }
}
class Customer
{
public string Region { get; set; }
public string CustomerID { get; set; }
public string CompanyName { get; set; }
public List<Order> Orders { get; set; }
}
class Order
{
public string OrderID { get; set; }
public DateTime OrderDate { get; set; }
}
留言
張貼留言