I would like to retrieve a List of elements as part of the compound key of the group by. But so far I have been unable to write the query to do it.
We are showing the total stock per RawMaterial (bellow more details for the model), and we would like to list alongside the suppliers for each RawMaterial. I tried this query:
var list = this.Stocks.Where(x => x.StockType == StockTypeEnum.RawMaterialEntered)
.GroupBy(i => new { RawMaterialId = i.RawMaterial.Id, RawMaterial = i.RawMaterial.Name,
Suppliers = i.RawMaterial.Suppliers.Select(j=> new { Id= j.Supplier.Id, Name = j.Supplier.Name}) })
.Select(g => new StockSummary
{
TotalAmount = g.Sum(i => i.Amount),
ComponentId = g.Key.RawMaterialId,
Component = g.Key.RawMaterial,
Suppliers = g.Key.Suppliers.Select(h=> new StockItemModel(){ Id = h.Id, Name = h.Name} ).ToList()
})
.OrderByDescending(g => g.Component).ToList();
It builds but when I try to run it, I get this exception:
A first chance exception of type 'System.NotImplementedException' occurred in NHibernate.dll
We tried different approaches, but nothing worked. Is it possible to obtain the desired result with just one query?
I have this model:
public class Stock
{
public RawMaterial RawMaterial{ get; set; }
public int Amount { get; set; }
}
public class RawMaterial
{
public int Id{ get;set; }
public string Name { get;set; }
public IList<SupplierInfo> SupplierInfos{ get;set; }
}
public class SupplierInfo
{
public int Id{ get;set; }
public Supplier Supplier { get;set; }
}
public class Supplier
{
public int Id { get;set; }
public string Name { get; set; }
}
Originally we had a grid where we show each the total stock available for a raw material. We retrieve that information with this query:
var list = this.Stocks.Where(x => x.StockType == StockTypeEnum.RawMaterialEntered)
.GroupBy(i => new { RawMaterialId = i.RawMaterial.Id, RawMaterial = i.RawMaterial.Name })
.Select(g => new StockSummary
{
TotalAmount = g.Sum(i => i.Amount),
ComponentId = g.Key.RawMaterialId,
Component = g.Key.RawMaterial,
})
.OrderByDescending(g => g.Component).ToList();
This data is shown in a table similar to this:
And now, as mentioned above, we would like to show a list of the suppliers that provided said components. Is it possible to do it all in one query?