我有一個表結構,每個表都有一個名爲「ID」的主鍵字段和與其父表的主鍵匹配的外鍵名。因此,表格下方有關係,他們的主鍵出現在另一個表和任何表中的第一場是它的主鍵:使用LoadWith「Deep loading」
Category
--------
CategoryID
Title
CategoryList
------------
CategoryListID
CategoryID
ListID
List
----
ListID
Title
DataPoint
---------
DataPointID
RecordedDateTime
DataPointValue
--------------
DataPointValueID
DataPointID
TheValue
上面是多到很多類別和清單之間的連接,通過所屬分類。它也是從List到DataPoint,DataPoint到DataPointValue的一對多連接。
使用C#/ LINQ和給出的類別ID值的列表,我想檢索:
所有連接到I類有ID的該列表條目。有了那些List條目,我想按照RecordedDateTime Descending命令來取最近的1個DataPoint。從那裏我想檢索連接到DataPoint的每個DataPointValue。
的LINQ我是:
DBDataContext上下文=新DBDataContext(的ConnectionString);
context.LoadOptions = new DataLoadOptions();
context.LoadOptions.LoadWith<DataPoint>(p => p.DataPoints.OrderByDescending(p.RecordedDataTime).FirstOrDefault());
// this next line is how I get the list of category IDs, but don't worry about that...
List<int> categoryIDs = (from TreeNode n in nodes
select Int32.Parse(n.Value)).Distinct().ToList();
var lists = from i in context.List
join ci in context.CategoryLists on i.ListID equals ci.ListID
join p in context.DataPoints on i.ListID equals p.ListID
join v in context.DataPointValues on p.DataPointID equals v.DataPointID
where categoryIDs.Contains(ci.CategoryID)
orderby i.Title ascending
select new
{
List = i,
DataPoint = p,
DataPointValues = p.DataPointValues
};
但是,這顯然不工作 - LoadWith導致我的問題。有人能解釋一下如何構建LoadWith,以便它可以儘可能少的SQL查詢來檢索這個(確實很大)的數據量嗎?
非常感謝,
馬特。