Linq up to 3 tables without foreign keys

I have 3 tables without foreign keys (this is an old db, so I cannot change this). The model will be something like this (sql code):

Select PROD.ProductoId, PROD.Descripcion, STK.StockActual, DEPO.DepositoId, DEPO.Descripcion From Productos PROD, Stock STOK, Depositos DEPO where PROD.ProductoId = STOK.ProductoId and DEPO.DepositoId = STOK.DepositoId 

How can I do to get the same results with Linq in C #?

+5
source share
3 answers

Try the following:

 var result = from prod in _context.Productos join stok in _context.Stocks on prod.ProductoId equals stok.ProductoId join depo in _context.Depositos on stok.DepositoId equals depo.DepositoId select new { ProductoId = prod.ProductoId, ProductoDescripcion = prod.Descripcion, StockActual = stok.StockActual, DepositoId = depo.DepositoId, DepositoDescripcion = depo.Descripcion }; 
+2
source

Why don't you create a View in Sql and then update the model so you can query from the View class.

+2
source

Try the following:

 var Query = from data in _db.Productos join stock in _db.Stock on data.ProductId equals stock.ProductoId join Depositos in _db.Depositos on data.DepositoId equals Depositos.DepositoId select new { ProductoId,Descripcion, StockActual,DepositoId,Descripcion}; 
+1
source

All Articles