I am using a LINQ left outer join on Entity Framework 7 RC1. We had to use a workaround as left joins are not properly implemented, see Left Outer Join with Entity Framework Core
var customers = from customer in _db.Customer
join postcode in _db.Postcode
on customer.PostcodeID equals postcode.PostcodeID into custPCTmp
from custPC in custPCTmp.DefaultIfEmpty()
select new
{
// Workaround for EF7 RC1.
Customer = customer,
CustPC = custPC
};
// Workaround for EF7 RC1.
model.CustomersList = new List<CustomerListItemViewModel>();
foreach (var cust in customers)
{
CustomerListItemViewModel custVM = new CustomerListItemViewModel()
{
CustomerID = cust.Customer.CustomerID,
Name = cust.Customer.Name,
Address = cust.Customer.Address,
Town = cust.CustPC == null ? string.Empty : cust.CustPC.Town,
Postcode = cust.CustPC == null ? string.Empty : cust.CustPC.Postcode
};
model.CustomersList.Add(custVM);
}
I am expecting a result like this (note the first 3 rows should have the same town and postcode):
Name Address Town Postcode
---------------------------------------
Name 1 Address 1 Town 1 Postcode 1
Name 2 Address 2 Town 1 Postcode 1
Name 3 Address 3 Town 1 Postcode 1
Name 4 Address 4 Town 4 Postcode 4
However I am getting the below result (duplication of first record, thrice):
Name Address Town Postcode
--------------------------------------
Name 1 Address 1 Town 1 Postcode 1
Name 1 Address 1 Town 1 Postcode 1
Name 1 Address 1 Town 1 Postcode 1
Name 4 Address 4 Town 4 Postcode 4
I can see it is due to the join of customer with postcode on PostcodeID, but why is it duplicating like this? The data in the database looks fine.
Kind regards,
Rasika