I am trying to work with the Join and GroupJoin method. The problem seems simple. Considering TableAand TableBhow data such as:
class MyDataContext : DataContext
{
public Table<tblA> TableA;
public Table<tblB> TableB;
}
... I use TableAas my main table and want to join a single field, CustomerIDin TableBfor retrieval [TableB].[LastName].
It should not be difficult, except that it is difficult for me to get results for the right job. There are records in Table A that I want, regardless of the corresponding CustomerID in TableB. Sounds like a left join - therefore, while reading here , I emulated what @tvanfosson suggested:
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 is dynamic, so one method creates a dynamic query:
public static void QueryStores()
{
IQueryable<tblA> source = myContext.TableA;
if (criteriaA)
source = source.Where(
if (criteriaB)
source = source.Where(
switch (byView)
{
case View1:
{
source = GetRecordsByView1(source);
break;
}
}
myGridView.DataSource = source;
}
Problem: I get the following error:
node 'OptionalValue' SQL.
, :
Owner = owner.Select(o => o.LastName).DefaultIfEmpty()
? GroupJoin .