我一直在尝试使用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