0
我正在使用nHibernate從Sql Server數據庫中檢索訂單集合(及其訂單行)。如何獲取應用父項和子項過濾器的兒童集合
這是我ShipmentOrder類:
public class ShipmentOrder
{
private ICollection<ShipmentDetail> _ShipmentsDetails;
public virtual ReadOnlyCollection<ShipmentDetail> ShipmentsDetails
{
get { return (new List<ShipmentDetail>(_ShipmentsDetails).AsReadOnly()); }
}
}
NHibernate的返回的IList的所有細節(ShipmentsDetails)加載(因爲我渴望加載它們)。
現在,我想過濾我的ShipmentOrder和ShipmentDetail的集合並獲取ShipmentDetail的集合。
我已經試過這樣的事情:
IList<ShipmentOrder> Results;
// Fetch orders using nHibernate
Results = FetchOrders();
var shipmentLines = Results
.Where(x => x.Company == "XXX" && x.OrderNumber == "111")
.SelectMany(x => x.ShipmentsDetails)
.Where(s => s.RowNumber == 1 && s.RowSeq == 0)
.ToList();
但我意識到,我獲得相同線路的多個結果。
我已經轉換LAMDA表達這樣的:
var shipmentLines = Results
.Where(x => x.Company == "XXX" && x.OrderNumber == "111")
.SelectMany(x => x.ShipmentsDetails)
.Where(s => s.RowNumber == 1 && s.RowSeq == 0)
.Distinct()
.ToList();
,它工作正常。 我想知道如果沒有截然不同的是否有更好的方法來實現相同的結果。
UPDATE:這裏
我使用引起的SelectMany這是我發現將過濾器應用於兒童(ShipmentsDetails)的唯一途徑。
喲想獲得一個清晰的列表,而不使用'Distinct'? – Jamiec
@Jamiec:那麼,我已經包含的示例代碼應該返回一行,但我得到4行,因爲有4個訂單。我正在使用SelectMany,因爲這是我發現在兒童收藏中應用過濾器的唯一方式。 – LeftyX
x'Company ==「XXX」&& x.OrderNumber ==「111」'是否確定一個'ShipmentOrder'?如果是這樣的話.RowNumber == 1 && s.RowSeq == 0'確定一個'ShipmentLine'?這個查詢的預期結果是什麼? – Jamiec