Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Linq to 3 tables with no foreign keys

I have 3 tables without foreign keys (it's a legacy db, so I can't change that). 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 on C#?

like image 748
Fryla- Cristian Marucci Avatar asked Jun 30 '15 02:06

Fryla- Cristian Marucci


1 Answers

Try this:

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
             };
like image 188
Felix Pamittan Avatar answered Oct 11 '22 04:10

Felix Pamittan