I have 3 tables that I am trying to combine them together using left joins. In my application, all LINQ expressions are in query-chain format (lambda expressions).
I am trying to figure out how to accomplish this query using LINQ;
This is my SQL query;
select i.INVOICE_NUMBER, ic.CustomerName, o.BusinessUnit
from Invoices_Daily as i
left join intercompany as ic on i.customer_number = ic.customernumber
left join ordertypes as o on i.LINE_ORDERTYPE = o.OrderType
I have tried combining the GroupJoin
as follows;
var commissions = data
.GroupJoin(genericNameList,
d => d.CUSTOMER_NUMBER,
g => g.CustomerId,
(d, g) => new { d, generic = g.FirstOrDefault() })
.GroupJoin(intercompanies,
dd => dd.d.CUSTOMER_NAME,
i => i.CustomerId,
(d, i) => new { data = d.d, intercompanies = i.FirstOrDefault() })
.Select(_ => new MainGridViewModel
{
INVOICE_DATE = _.d.INVOICE_DATE,
EndCustomer = _.generic == null ? _.d.CUSTOMER_NAME : _.generic.EndCustomer ?? _.d.CUSTOMER_NAME,
LINE_ORDERTYPE = _.i.OrderType
})
.ToList();
However I am having a syntax error that is written below;
Error CS0411 The type arguments for method 'Enumerable.GroupJoin(IEnumerable, IEnumerable, Func, Func, Func, TResult>)' cannot be inferred from the usage. Try specifying the type arguments explicitly.
CUSTOMER_NAME
andCustomerId
) have different types. Also note that(d, i) => new { data = d.d, intercompanies = i.FirstOrDefault() })
would be translated to something likeouter apply (select top 1 ...)
instead ofleft join
. – tukaefCUSTOMER_NAME
andCustomerId
are bothstring
type. When I useGroupJoin
with theFirstOrDefault
it always brings me the same results as I do left join on SQL side. I never encountered any issues yet. Its my first time tying to combine multiple tables... – 0014GroupJoin
. In fact the intellisense does not give any results when I typei => i.
on the secondGroupJoin
where I am expecting the IDE to bring all the columns forintercompanies
. – 0014