[LINQ] Where 查詢語句
有關where的查詢表達式可參考以下:
http://msdn.microsoft.com/zh-tw/library/bb301979.aspx
首先創建我們要查詢的類別
[Serializable]
public class Customer
{
public int ID { get; set; }
public string CompanyName { get; set; }
public string City { get; set; }
public string Region { get; set; }
public string Country { get; set; }
public List<Order> Orders { get; set; }
}
[Serializable]
public class Order
{
public int ID { get; set; }
public int CustomerID { get; set; }
public DateTime? OrderDate { get; set; }
}
初始化集合假資料
private List<Customer> _customerList = new List<Customer>()
{
new Customer() { CompanyName = "台雞店", City = "台北", Country = "台灣", ID = 1,
Orders =new List<Order>()
{
new Order(){ID = 1,CustomerID = 1,OrderDate = DateTime.Now.AddDays(1)},
new Order(){ID = 2,CustomerID = 1,OrderDate = DateTime.Now.AddDays(2)},
new Order(){ID = 3,CustomerID = 1,OrderDate = DateTime.Now.AddDays(5)},
}},
new Customer() { CompanyName = "頂泰風", City = "天龍", Country = "台灣", ID = 2 },
new Customer() { CompanyName = "打了沒", City = "台北", Country = "台灣", ID = 3 },
new Customer() { CompanyName = "台灣水妮", City = "高雄", Country = "台灣", ID = 4 ,
Orders =new List<Order>()
{
new Order(){ID = 1,CustomerID = 4,OrderDate = DateTime.Now.AddDays(1).AddHours(1)},
new Order(){ID = 2,CustomerID = 4,OrderDate = DateTime.Now.AddDays(3).AddDays(2)},
new Order(){ID = 3,CustomerID = 4,OrderDate = DateTime.Now.AddDays(5).AddMonths(3)},
}},
new Customer() { CompanyName = "老是穿", City = "高雄", Country = "台灣", ID = 5 },
new Customer() { CompanyName = "酊亡", City = "高雄", Country = "台灣", ID = 6 ,
Orders =new List<Order>()
{
new Order(){ID = 1,CustomerID = 6,OrderDate = DateTime.Now.AddDays(3)},
new Order(){ID = 2,CustomerID = 6,OrderDate = DateTime.Now.AddDays(7).AddHours(1)},
new Order(){ID = 3,CustomerID = 6,OrderDate = DateTime.Now.AddDays(10).AddMonths(1)},
}},
};
找出含有City==”台北"的物件
法一:
var query = from customer in this._customerList
where customer.City == "台北"
select customer;
foreach (var customer in query)
{
Console.WriteLine("CompanyName : " + customer.CompanyName);
}
result:
CompanyName : 台雞店
CompanyName : 打了沒
法二:
var query = this._customerList.Where(c => c.City == "台北");
foreach (var customer in query)
{
Console.WriteLine("CompanyName : " + customer.CompanyName);
}
result:
CompanyName : 台雞店
CompanyName : 打了沒
找出含有City==”台北" && 索引值<=3
法一:使用List.IndexOf
var query = from customer in this._customerList
where customer.City == "台北" && this._customerList.IndexOf(customer) <= 3
select customer;
foreach (var customer in query)
{
Console.WriteLine("CompanyName : " + customer.CompanyName);
}
result: CompanyName : 台雞店 CompanyName : 打了沒
法二:
var query = this._customerList.Where((c, index) => c.City == "台北" && index <= 3);
foreach (var customer in query)
{
Console.WriteLine("CompanyName : " + customer.CompanyName);
}
result:
CompanyName : 台雞店
CompanyName : 打了沒
找出含有City==”台北" && 索引值<=3,在第一筆就離開查詢
法一:
var query = (from customer in this._customerList
where customer.City == "台北" && this._customerList.IndexOf(customer) <= 3
select customer).FirstOrDefault();
if (query != null)
{
Console.WriteLine("CompanyName : " + query.CompanyName);
}
result: CompanyName : 台雞店
法二:
var query = this._customerList.Where((c, index) => c.City == "台北" && index <= 3).FirstOrDefault();
if (query != null)
{
Console.WriteLine("CompanyName : " + query.CompanyName);
}
result: CompanyName : 台雞店
若有謬誤,煩請告知,新手發帖請多包涵
Microsoft MVP Award 2010~2017 C# 第四季
Microsoft MVP Award 2018~2022 .NET