当前位置 : 主页 > 编程语言 > c语言 >

在VB.net中使用Linq编写连接查询

来源:互联网 收集:自由互联 发布时间:2021-06-24
我试图将SQL查询转换为vb中的 linq查询.但我在使语法正确方面遇到了一些麻烦. 原始查询 SELECT b.* FROM History_Table_B b INNER JOIN Employee e ON b.EmployeeId = e.EmployeeId INNER JOIN Company c ON e.CompanyId =
我试图将SQL查询转换为vb中的 linq查询.但我在使语法正确方面遇到了一些麻烦.

原始查询

SELECT b.* 
FROM History_Table_B b 
INNER JOIN Employee e ON b.EmployeeId = e.EmployeeId 
INNER JOIN Company c ON e.CompanyId = c.CompanyId 
WHERE e.AncillaryId = @AncillaryPersonId 
AND c.AncillaryId = @AncillaryCompanyId
AND (b.EndDate is null OR b.EndDate >= convert(date, GetDate()))

我的Linq

Dim result = From b In context.H_Table_B
             Join employee In context.Employees
             On b.EmployeeId Equals (employee.EmployeeId)
             Join company In context.Companies
             On employee.CompanyId Equals (company.CompanyId) 
             Where employee.AncillaryId Equals(iPerId) 
             And company.AncillaryId Equals (iCompanyId) 
             And ((b.EndDate Is Nothing) Or (b.EndDate Equals(DateTime.Today)))
在何处条件下,您不能使用Equals(Operator),例如Join LINQ query.这里Equals是一个对象类的方法,因此您可以使用’.’进行访问.例如employee.AncillaryId.Equals(iCompanyId)

另外还有一件事,在新线VB.net的Where条件中需要’_’.

例如

From b In context.H_Table_B
         Join employee In context.Employees
         On b.EmployeeId Equals (employee.EmployeeId)
         Join company In context.Companies
         On employee.CompanyId Equals (company.CompanyId) 
         Where employee.AncillaryId.Equals(iPerId) _ 
         And company.AncillaryId.Equals(iCompanyId) _
         And ((b.EndDate Is Nothing) Or (b.EndDate.Equals(DateTime.Today)))
网友评论