Ho una query che combina un join e un gruppo, ma ho un problema. La query è come:
var result = from p in Products join bp in BaseProducts on p.BaseProductId equals bp.Id group p by p.SomeId into pg select new ProductPriceMinMax { SomeId = pg.FirstOrDefault().SomeId, CountryCode = pg.FirstOrDefault().CountryCode, MinPrice = pg.Min(m => m.Price), MaxPrice = pg.Max(m => m.Price), BaseProductName = bp.Name <------ can't use bp. };
Come puoi vedere, si unisce alla tabella Prodotti con la tabella BaseProducts e si raggruppa su un id della tabella Product. Ma nel risultante ProductPriceMinMax, ho anche bisogno di una proprietà della tabella BaseProducts: bp.Name, ma non sa bp.
Qualche idea su cosa sto facendo male?
Grazie!
Una volta che hai fatto questo
group p by p.SomeId into pg
non hai più accesso alle variabili di intervallo utilizzate nell’iniziale from
. Cioè, non puoi più parlare di p
o bp
, puoi solo parlare di pg
.
Ora, pg
è un gruppo e quindi contiene più di un prodotto. Tutti i prodotti in un determinato gruppo pg
hanno lo stesso SomeId
(poiché è quello che hai raggruppato), ma non so se questo significa che hanno tutti lo stesso BaseProductId
.
Per ottenere un nome di prodotto di base, devi selezionare un particolare prodotto nel gruppo pg
(come stai facendo con SomeId
e CountryCode
), quindi unirti a BaseProducts
.
var result = from p in Products group p by p.SomeId into pg // join *after* group join bp in BaseProducts on pg.FirstOrDefault().BaseProductId equals bp.Id select new ProductPriceMinMax { SomeId = pg.FirstOrDefault().SomeId, CountryCode = pg.FirstOrDefault().CountryCode, MinPrice = pg.Min(m => m.Price), MaxPrice = pg.Max(m => m.Price), BaseProductName = bp.Name // now there is a 'bp' in scope };
Detto questo, sembra piuttosto insolito e penso che dovresti fare un passo indietro e considerare cosa stai cercando di recuperare.
Lo abbiamo fatto in questo modo:
from p in Products join bp in BaseProducts on p.BaseProductId equals bp.Id where !string.IsNullOrEmpty(p.SomeId) && p.LastPublished >= lastDate group new { p, bp } by new { p.SomeId } into pg let firstproductgroup = pg.FirstOrDefault() let product = firstproductgroup.p let baseproduct = firstproductgroup.bp let minprice = pg.Min(m => mpPrice) let maxprice = pg.Max(m => mpPrice) select new ProductPriceMinMax { SomeId = product.SomeId, BaseProductName = baseproduct.Name, CountryCode = product.CountryCode, MinPrice = minprice, MaxPrice = maxprice };
EDIT: abbiamo usato la versione di AakashM, perché ha prestazioni migliori
Ho incontrato lo stesso problema di te.
Spingo i result
due tables
nell’object t1
e nel gruppo t1
.
from p in Products join bp in BaseProducts on p.BaseProductId equals bp.Id select new { p, bp } into t1 group t1 by t1.p.SomeId into g select new ProductPriceMinMax { SomeId = g.FirstOrDefault().p.SomeId, CountryCode = g.FirstOrDefault().p.CountryCode, MinPrice = g.Min(m => m.bp.Price), MaxPrice = g.Max(m => m.bp.Price), BaseProductName = g.FirstOrDefault().bp.Name };