我一直在嘗試使用Join和GroupJoin方法。問題看起來很簡單。鑑於TableA
和TableB
的數據地圖這樣的:正確加入/組加入執行
class MyDataContext : DataContext
{
public Table<tblA> TableA;
public Table<tblB> TableB;
}
...我使用TableA
作爲我的主表,並想加入一個領域,在CustomerID
TableB
檢索[TableB].[LastName]
。
應該不困難,除非我難以使結果正常工作。無論TableB中匹配的CustomerID如何,TableA都有我想要的記錄。聽起來像一個左連接 - 所以,閱讀here,我模仿什麼@tvanfosson建議:
// appropriately rewritten for my needs - so I thought...
private static IQueryable GetRecordsByView1(IQueryable<tblA> source)
{
var records = source.GroupJoin(myContext.TableB,
info => info.CustomerID,
owner => owner.CustomerID,
(info, owner) => new
{
info.CustomerID,
Owner = owner.Select(o => o.LastName).DefaultIfEmpty(),
Store = info.Store,
})
.Select(record => new
{
record.CustomerID,
record.Owner,
record.Store,
});
return records;
}
source
是動態的,這樣的一個方法生成動態查詢:
public static void QueryStores()
{
IQueryable<tblA> source = myContext.TableA;
if (criteriaA)
source = source.Where(// something);
if (criteriaB)
source = source.Where(// something);
// after processing criteria logic, determine type of view
switch (byView)
{
case View1:
{
source = GetRecordsByView1(source);
break;
}
//other case blocks
}
myGridView.DataSource = source;
}
的問題:我接收以下錯誤:
Could not format node 'OptionalValue' for execution as SQL.
我相信這是在下面的代碼行:
Owner = owner.Select(o => o.LastName).DefaultIfEmpty()
我在這裏做錯了什麼?我必須寫作GroupJoin
作爲擴展方法。
的問題是,我必須寫我的'Join'作爲一個擴展方法。我不能寫成LINQ語句。 – IAbstract 2010-11-08 15:15:43