I’m working on an ASP.NET MVC project and need help converting a SQL inner join query to LINQ to SQL syntax. Here’s my original SQL query:
select P.Product_id, P.Product_Name, U.UnitName as Unit, P.Product_Category as Details
from Products as P inner join UnitTypes as U
on P.UnitTypeId = U.Id where P.Is_Active=1
Currently my repository class looks like this:
public class ProductRepository
{
private AppDataContext context = new AppDataContext();
public IQueryable<Product> GetAllProducts()
{
return context.Products;
}
}
The issue is that context.Products
only returns data from one table. I need to join it with another table and return the combined results. Here’s what I want to achieve:
public class ProductRepository
{
private AppDataContext context = new AppDataContext();
public IQueryable<Product> GetAllProducts()
{
// need inner join implementation here
}
}
What’s the correct way to write this inner join query in LINQ to SQL?