INQ(Language Integrated Query)是C#中用于处理数据的强大工具。它使得对集合、数据库、XML等数据源的查询更加简洁和可读。
using System;
using System.Collections.Generic;
using System.Linq;
public class LINQExample
{
public static void Main(string[] args)
{
List numbers = new List { 5, 10, 15, 20, 25 };
// 使用方法语法
var filteredNumbers = numbers.Where(n => n > 10).OrderBy(n => n);
foreach (var num in filteredNumbers)
{
Console.WriteLine(num); // 输出:15, 20, 25
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
public class LINQExample
{
public static void Main(string[] args)
{
List numbers = new List { 5, 10, 15, 20, 25 };
// 使用查询表达式语法
var filteredNumbers = from n in numbers
where n > 10
orderby n
select n;
foreach (var num in filteredNumbers)
{
Console.WriteLine(num); // 输出:15, 20, 25
}
}
}
使用group by对元素分组。
List words = new List { "apple", "banana", "apricot", "cherry" };
var groupedWords = from word in words
group word by word[0] into g
select new { FirstLetter = g.Key, Words = g };
foreach (var item in groupedWords)
{
Console.WriteLine($"Words starting with {item.FirstLetter}: {string.Join(", ", item.Words)}");
}
使用Join连接两个集合。
List students = new List { "Alice", "Bob", "Charlie" };
List scores = new List { 85, 90, 78 };
var studentScores = students.Zip(scores, (student, score) => new { Student = student, Score = score });
foreach (var entry in studentScores)
{
Console.WriteLine($"{entry.Student}: {entry.Score}");
}
给定一个整数列表,筛选出所有的偶数并按升序排列,然后输出这些数字。
using System;
using System.Collections.Generic;
using System.Linq;
public class EvenNumbersExample
{
public static void Main(string[] args)
{
List numbers = new List { 5, 10, 15, 20, 25, 30 };
// LINQ 查询:过滤偶数并排序
var evenNumbers = numbers.Where(n => n % 2 == 0).OrderBy(n => n);
Console.WriteLine("Even numbers:");
foreach (var num in evenNumbers)
{
Console.WriteLine(num); // 输出:10, 20, 30
}
}
}
创建一个包含多个学生及其成绩的字典,查询出成绩高于80的学生名字,并按成绩降序排列。
using System;
using System.Collections.Generic;
using System.Linq;
public class StudentScoresExample
{
public static void Main(string[] args)
{
Dictionary studentScores = new Dictionary
{
{ "Alice", 85 },
{ "Bob", 90 },
{ "Charlie", 78 },
{ "David", 92 }
};
var topStudents = studentScores
.Where(s => s.Value > 80)
.OrderByDescending(s => s.Value)
.Select(s => s.Key);
Console.WriteLine("Top students:");
foreach (var student in topStudents)
{
Console.WriteLine(student); // 输出:David, Bob, Alice
}
}
}
给定一组单词,将它们按首字母分组,并统计每组中单词的数量。
using System;
using System.Collections.Generic;
using System.Linq;
public class GroupWordsExample
{
public static void Main(string[] args)
{
List words = new List { "apple", "apricot", "banana", "cherry", "avocado", "blueberry" };
var groupedWords = from word in words
group word by word[0] into g
select new { FirstLetter = g.Key, Count = g.Count() };
Console.WriteLine("Grouped words by first letter:");
foreach (var group in groupedWords)
{
Console.WriteLine($"Letter: {group.FirstLetter}, Count: {group.Count}");
// 输出:
// Letter: a, Count: 3
// Letter: b, Count: 2
// Letter: c, Count: 1
}
}
}
这些例子展示了如何利用LINQ来进行数据查询、排序、分组和聚合操作。如有其他问题或需要进一步讲解,请随时联系我!