LINQ(Language-Integrated Query)是C#語言中的一個強大的查詢技術,它提供了一種統一的查詢語法,可以用於查詢和操作各種數據源,包括集合、資料庫、XML等。下麵詳細描述了LINQ的20個功能點,並提供了相應的源代碼。 查詢集合中的所有元素: int[] numbers = ...
LINQ(Language-Integrated Query)是C#語言中的一個強大的查詢技術,它提供了一種統一的查詢語法,可以用於查詢和操作各種數據源,包括集合、資料庫、XML等。下麵詳細描述了LINQ的20個功能點,並提供了相應的源代碼。
- 查詢集合中的所有元素:
int[] numbers = { 1, 2, 3, 4, 5 };
var query = from num in numbers
select num;
foreach (var num in query)
{
Console.WriteLine(num);
}
- 使用條件過濾集合中的元素:
int[] numbers = { 1, 2, 3, 4, 5 };
var query = from num in numbers
where num % 2 == 0
select num;
foreach (var num in query)
{
Console.WriteLine(num);
}
- 對集合中的元素進行排序:
int[] numbers = { 5, 3, 1, 4, 2 };
var query = from num in numbers
orderby num
select num;
foreach (var num in query)
{
Console.WriteLine(num);
}
- 對集合中的元素進行分組:
string[] names = { "Alice", "Bob", "Charlie", "David", "Eve" };
var query = from name in names
group name by name[0];
foreach (var group in query)
{
Console.WriteLine(group.Key);
foreach (var name in group)
{
Console.WriteLine(name);
}
}
- 對集合中的元素進行投影:
string[] names = { "Alice", "Bob", "Charlie", "David", "Eve" };
var query = from name in names
select name.Length;
foreach (var length in query)
{
Console.WriteLine(length);
}
- 連接兩個集合的元素:
string[] names1 = { "Alice", "Bob", "Charlie" };
string[] names2 = { "David", "Eve", "Frank" };
var query = from name1 in names1
join name2 in names2 on name1[0] equals name2[0]
select new { Name1 = name1, Name2 = name2 };
foreach (var pair in query)
{
Console.WriteLine($"{pair.Name1}, {pair.Name2}");
}
- 使用LINQ查詢資料庫:
using (var dbContext = new MyDbContext())
{
var query = from user in dbContext.Users
where user.Age > 18
select user;
foreach (var user in query)
{
Console.WriteLine(user.Name);
}
}
- 使用LINQ查詢XML文檔:
XDocument doc = XDocument.Load("data.xml");
var query = from element in doc.Descendants("book")
where (int)element.Element("price") > 10
select element.Element("title").Value;
foreach (var title in query)
{
Console.WriteLine(title);
}
- 使用LINQ查詢JSON數據:
string json = File.ReadAllText("data.json");
var data = JsonConvert.DeserializeObject<List<Person>>(json);
var query = from person in data
where person.Age > 18
select person.Name;
foreach (var name in query)
{
Console.WriteLine(name);
}
- 使用LINQ查詢並轉換結果:
int[] numbers = { 1, 2, 3, 4, 5 };
var query = from num in numbers
where num % 2 == 0
select num * 2;
foreach (var num in query)
{
Console.WriteLine(num);
}
- 使用LINQ查詢返回匿名類型:
string[] names = { "Alice", "Bob", "Charlie", "David", "Eve" };
var query = from name in names
select new { Name = name, Length = name.Length };
foreach (var item in query)
{
Console.WriteLine($"{item.Name}, {item.Length}");
}
- 使用LINQ查詢返回自定義類型:
public class Person
{
public string Name { get; set; }
public int Age { get; set; }
}
List<Person> people = new List<Person>
{
new Person { Name = "Alice", Age = 20 },
new Person { Name = "Bob", Age = 25 },
new Person { Name = "Charlie", Age = 30 }
};
var query = from person in people
where person.Age > 25
select person.Name;
foreach (var name in query)
{
Console.WriteLine(name);
}
- 使用LINQ查詢返回集合:
int[] numbers = { 1, 2, 3, 4, 5 };
var query = from num in numbers
where num % 2 == 0
select num * 2;
List<int> result = query.ToList();
foreach (var num in result)
{
Console.WriteLine(num);
}
- 使用LINQ進行分頁查詢:
int pageNumber = 2;
int pageSize = 10;
var query = from product in dbContext.Products
orderby product.Name
select product;
var page = query.Skip((pageNumber - 1) * pageSize).Take(pageSize);
foreach (var product in page)
{
Console.WriteLine(product.Name);
}
- 使用LINQ進行聚合操作:
int[] numbers = { 1, 2, 3, 4, 5 };
int sum = numbers.Sum();
int max = numbers.Max();
int min = numbers.Min();
double average = numbers.Average();
Console.WriteLine($"Sum: {sum}");
Console.WriteLine($"Max: {max}");
Console.WriteLine($"Min: {min}");
Console.WriteLine($"Average: {average}");
- 使用LINQ進行集合的交集操作:
int[] numbers1 = { 1, 2, 3, 4, 5 };
int[] numbers2 = { 4, 5, 6, 7, 8 };
var query = numbers1.Intersect(numbers2);
foreach (var num in query)
{
Console.WriteLine(num);
}
- 使用LINQ進行集合的並集操作:
int[] numbers1 = { 1, 2, 3, 4, 5 };
int[] numbers2 = { 4, 5, 6, 7, 8 };
var query = numbers1.Union(numbers2);
foreach (var num in query)
{
Console.WriteLine(num);
}
- 使用LINQ進行集合的差集操作:
int[] numbers1 = { 1, 2, 3, 4, 5 };
int[] numbers2 = { 4, 5, 6, 7, 8 };
var query = numbers1.Except(numbers2);
foreach (var num in query)
{
Console.WriteLine(num);
}
- 使用LINQ進行集合的連接操作:
var query = from order in dbContext.Orders
join customer in dbContext.Customers on order.CustomerId equals customer.Id
select new { OrderId = order.Id, CustomerName = customer.Name };
foreach (var result in query)
{
Console.WriteLine($"{result.OrderId}, {result.CustomerName}");
}
- 使用LINQ進行集合的逆序操作:
int[] numbers = { 1, 2, 3, 4, 5 };
var query = numbers.Reverse();
foreach (var num in query)
{
Console.WriteLine(num);
}
以上是20個常用的LINQ功能點的示例代碼,它們展示了LINQ在C#中的強大查詢能力和靈活性。通過使用LINQ,開發人員可以更加方便地進行各種數據操作和查詢,提高代碼的可讀性和開發效率。