0%

LINQ查詢語法

LINQ介紹

語言整合查詢(英語:Language Integrated Query,縮寫:LINQ)是Microsoft的一種查詢語法技術,C#、VB都可以使用

LINQ 語法

Where

1
2
3
4
// filter odd number
int[] numbers = { 5, 10, 8, 3, 6, 12};
IEnumerable<int> numQuery2 = numbers.Where(num => num % 2 == 0)
// Output: 10 8 6 12

OrderBy

1
2
3
4
// filter odd number
int[] numbers = { 5, 10, 8, 3, 6, 12};
IEnumerable<int> numQuery2 = numbers.Where(num => num % 2 == 0).OrderBy(n => n);
// Output:6 8 10 12

Contains

1
2
3
4
5
6
string[] fruits = { "apple", "banana", "mango", "orange", "passionfruit", "grape" };

string fruit = "mango";

bool hasMango = fruits.Contains(fruit);
// hasMango=true

Join

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
class Product
{
public string Name { get; set; }
public int CategoryID { get; set; }
}

class Category
{
public string Name { get; set; }
public int ID { get; set; }
}

class ProductWithCategoryName
{
public string ProductName { get; set; }
public string CategoryName { get; set; }
}
static void Main(string[] args)
{
List<Category> categories = new List<Category>()
{
new Category {Name="Beverages", ID=001},
new Category {Name="Condiments", ID=002},
new Category {Name="Vegetables", ID=003},
new Category {Name="Grains", ID=004},
new Category {Name="Fruit", ID=005}
};

// Specify the second data source.
List<Product> products = new List<Product>()
{
new Product {Name="Cola", CategoryID=001},
new Product {Name="Tea", CategoryID=001},
new Product {Name="Mustard", CategoryID=002},
new Product {Name="Pickles", CategoryID=002},
new Product {Name="Carrots", CategoryID=003},
new Product {Name="Bok Choy", CategoryID=003},
new Product {Name="Peaches", CategoryID=005},
new Product {Name="Melons", CategoryID=005},
};
var productWithCategoryNameList = products.Join(categories,
p => p.CategoryID,
c => c.ID,
(p, c) => new ProductWithCategoryName
{
ProductName = p.Name,
CategoryName = c.Name,
}.ToList());
// productWithCategoryNameList=
// ProductName:Cola ,CategoryName:Beverages
// ProductName:Tea ,CategoryName:Beverages
// ProductName:Mustard ,CategoryName:Condiments
// ProductName:Pickles ,CategoryName:Condiments
// ProductName:Carrots ,CategoryName:Vegetables
// ProductName:Bok Choy ,CategoryName:Vegetables
// ProductName:Peaches ,CategoryName:Fruit
// ProductName:Melons ,CategoryName:Fruit
}

語法等同於:SELECT * FROM Product JOIN Category ON Product.CategoryID=Category.ID

也可以在LINQ語法中寫入function

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
class ProductAndCategoryName
{
public string Name { get; set; }
}

var ConcatNameList = products.Join(categories,
p => p.CategoryID,
c => c.ID,
(p, c) =>
{
string str = p.Name + "_" + c.Name;
return new ProductAndCategoryName
{
Name = str,
};
});
// ConcatNameList:
// Name:Cola_Beverages
// Name:Tea_Beverages
// Name:Mustard_Condiments
// Name:Pickles_Condiments
// Name:Carrots_Vegetables
// Name:Bok Choy_Vegetables
// Name:Peaches_Fruit
// Name:Melons_Fruit

第一個資料來源.Join(第二個資料來源,
one => 要比較欄位,
two => 要比較欄位,
(one, two) => {function})

ForEach

1
2
3
4
5
6
7
8
9
10
11
12
13
14
List<Category> categories = new List<Category>()
{
new Category {Name="Beverages", ID=001},
new Category {Name="Condiments", ID=002},
new Category {Name="Vegetables", ID=003},
new Category {Name="Grains", ID=004},
new Category {Name="Fruit", ID=005}
};
categories.ForEach(e => Console.WriteLine(e.Name));
// Beverages
// Condiments
// Vegetables
// Grains
// Fruit