我想檢索作爲組的組合鍵的一部分的元素列表。但到目前爲止,我一直無法編寫查詢來完成它。如何將列表添加到Linq to Sql組中的鍵由
我們正在展示每個RawMaterial的總庫存數量(以下爲該模型的更多詳細信息),我們希望列出每個RawMaterial的供應商。 我嘗試此查詢:
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();
它建立,但是當我嘗試運行它,我得到這個異常:
型「System.NotImplementedException」 發生的NHibernate的第一個機會異常。 dll
我們嘗試了不同的方法,但沒有奏效。只用一個查詢就可以獲得想要的結果嗎?
我有這樣的模式:
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; }
}
本來我們有我們顯示可用的原料各總存量的網格。我們檢索與此查詢信息:
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();
該數據顯示在一個表與此類似:
而現在,如上文提到的,我們想展現的列表提供所述組件的供應商。是否可以在一個查詢中完成所有操作?
你正在尋找一個休眠解決方案或一個SQL Server解決方案?我無法開始幫助您使用hibernate,但在sql server中,您可以輕鬆使用這些東西和xml。看看這裏。 http://stackoverflow.com/questions/451415/simulating-group-concat-mysql-function-in-microsoft-sql-server-2005 –
你是否在GroupBy調用中按列表分組數據?據我所知,NHibernate和EF都不支持這種操作。 –
我會說不,你不能在一個查詢中做到這一點。獲得第一個查詢所需的所有結果,將它們全部從數據庫加載並將它們分組到應用程序中。我不確定你想要做什麼甚至可能在SQL中。 – Sidewinder94