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)))
relational tables. In a LINQ query expression, join operations are performed on object collections. Object collections cannot be "joined" in exactly the same way as two relational tables.
The Language Integrated Query (LINQ), which is pronounced as “link”, was introduced in the . NET Framework 3.5 to provide query capabilities by defining standardized query syntax in the . NET programming languages (such as C# and VB.NET). LINQ is provided via the System.
LINQ queries are always executed when the query variable is iterated over, not when the query variable is created. This is called deferred execution. You can also force a query to execute immediately, which is useful for caching query results.
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)))
I think you're just missing a dot - try:
...b.EndDate.Equals(DateTime.Today)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With