5

我试图在 vb 中将 SQL 查询转换为 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)))
4

2 回答 2

6

在 where 条件下,您不能使用像 Join LINQ 查询这样的 Equals(运算符)。这里 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)))
于 2012-09-13T16:42:11.147 回答
2

我认为您只是缺少一个点-尝试:

...b.EndDate.Equals(DateTime.Today)
于 2012-09-13T16:35:10.077 回答