Writing Join Query Using Linq In VB.net

entity-framework join linq vb.net

Question

Im trying to convert a SQL Query to a linq query in vb. But Im have some trouble getting the syntax correct.

Original Query

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()))

My 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)))
1
5
9/13/2012 4:55:23 PM

Accepted Answer

On where condition you can not use Equals (Operator) like Join LINQ query. Here Equals is a method of object class so you can access using '.' e.g. employee.AncillaryId.Equals(iCompanyId)

And Also one more thing in Where condition for new line VB.net required '_'.

e.g.

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)))
6
9/13/2012 4:59:13 PM

Popular Answer

I think you're just missing a dot - try:

...b.EndDate.Equals(DateTime.Today)


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow