在VB.NET中,LINQ(Language Integrated Query)提供了丰富的查询功能,使得编写针对各种数据源的查询变得简单且一致。以下是LINQ在VB.NET中几个实用的用法,结合具体实例进行详细说明:
1. 筛选数据(Where子句)
作用:根据给定条件从数据源中筛选出符合条件的元素。
实例:
假设有一个Students
列表,包含学生对象,要找出所有年龄大于18岁的学生:
Dim students = New List(Of Student)()
' 假设 students 已经填充了数据...
Dim adults = From student In students
Where student.Age > 18
Select student
' 或使用 Lambda 表达式:
' Dim adults = students.Where(Function(s) s.Age > 18)
' adults 现在包含了所有年龄大于18岁的学生对象。
2. 排序数据(OrderBy/OrderByDescending)
作用:对数据源中的元素进行升序或降序排序。
实例:
对于一个Products
列表,按照产品名称升序排序:
Dim products = New List(Of Product)()
' 假设 products 已经填充了数据...
Dim sortedProducts = From product In products
Order By product.Name
Select product
' 或使用 Lambda 表达式:
' Dim sortedProducts = products.OrderBy(Function(p) p.Name)
' sortedProducts 现在包含了按名称升序排列的产品。
03. 分组数据(GroupBy)
作用:将数据源中的元素按照指定属性进行分组。
实例:
对于一个Sales
列表,按月份分组销售记录,并计算各月的总销售额:
Dim sales = New List(Of Sale)()
' 假设 sales 已经填充了数据...
Dim salesByMonth = From sale In sales
Group By Month = sale.Date.Month Into g
Select Month, TotalSales = g.Sum(Function(s) s.Amount)
' 或使用 Lambda 表达式:
' Dim salesByMonth = sales.GroupBy(Function(s) s.Date.Month)
' .Select(Function(g) New With {
' .Month = g.Key,
' .TotalSales = g.Sum(Function(s) s.Amount)
' })
' salesByMonth 现在是一个集合,每个元素代表一个月份及其对应总销售额。
4. 聚合数据(Sum、Average、Max、Min)
作用:对分组后的数据或整个数据源执行聚合计算。
实例:
继续上一个分组的例子,计算所有月份的总销售额:
Dim totalSales = salesByMonth.Sum(Function(s) s.TotalSales)
' 或直接对原始 sales 列表进行计算:
' Dim totalSales = sales.Sum(Function(s) s.Amount)
' totalSales 现在存储了所有销售记录的总金额。
5. 联接数据(Join)
作用:根据共享键将两个或多个数据源联接在一起。
实例:
假设有两个列表,Employees
和Departments
,要找出每个员工所在的部门名称:
Dim employees = New List(Of Employee)()
Dim departments = New List(Of Department)()
' 假设 employees 和 departments 已经填充了数据...
Dim employeeDepartments = From emp In employees
Join dept In departments On emp.DepartmentID Equals dept.ID
Select EmployeeName = emp.Name, DepartmentName = dept.Name
' 或使用 Lambda 表达式:
' Dim employeeDepartments = employees.Join(departments,
' Function(emp) emp.DepartmentID,
' Function(dept) dept.ID,
' Function(emp, dept) New With {
' .EmployeeName = emp.Name,
' .DepartmentName = dept.Name
' })
' employeeDepartments 现在包含了员工姓名与对应部门名称的配对。
6. 嵌套查询与子查询
作用:在一个查询内部嵌套另一个查询,或者将一个查询作为另一个查询的一部分。
实例:
找出每个部门中薪水最高的员工:
Dim employees = New List(Of Employee)()
' 假设 employees 已经填充了数据...
Dim topEmployeesPerDepartment = From dept In departments
Let topEmployee = (From emp In employees
Where emp.DepartmentID = dept.ID
Order By emp.Salary Descending
Select emp).FirstOrDefault()
Where topEmployee IsNot Nothing
Select DepartmentName = dept.Name, TopEmployee = topEmployee
' 或使用 Lambda 表达式:
' Dim topEmployeesPerDepartment = departments.Select(Function(d) New With {
' .DepartmentName = d.Name,
' .TopEmployee = employees.Where(Function(e) e.DepartmentID = d.ID)
' .OrderByDescending(Function(e) e.Salary)
' .FirstOrDefault()
' })
' topEmployeesPerDepartment 现在包含了每个部门及其薪水最高的员工。
7. 使用Lambda表达式进行延迟执行与即时执行
作用:控制何时实际执行查询操作,以优化性能或确保预期结果。
实例:
延迟执行(Lazy Evaluation):查询直到被迭代时才执行:
Dim lazyQuery = products.Where(Function(p) p.Category = "Electronics")
' 此时查询未被执行
For Each product In lazyQuery
Console.WriteLine(product.Name)
Next
' 在循环中遍历时查询才被执行
即时执行(Eager Evaluation):使用ToList()
、ToArray()
等方法立即执行查询并返回结果:
Dim eagerResult = products.Where(Function(p) p.Category = "Electronics").ToList()
' 查询在此处立即执行并返回列表
For Each product In eagerResult
Console.WriteLine(product.Name)
Next