0

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

Community
  • 1
  • 1
rasika
  • 131
  • 1
  • 6
  • Who knows. If there is one bug, maybe this is another one. I'm not too impressed by this RC1. I don't know why you need this work-around. I hope you're not relying on EF7 for production code. – Gert Arnold May 14 '16 at 22:35
  • We tried various approaches and all but this gave us the "Sequence contains no elements" error (please see link in the question). – rasika May 14 '16 at 23:05
  • Yes I know why you need the work-around *technically*. I meant to ask why you need it for your project. Can't you just leave it and wait until it's solved? – Gert Arnold May 14 '16 at 23:08
  • Can do, but it would be better to get a solution sooner rather than later. – rasika May 14 '16 at 23:10
  • I've seen too many work-arounds stick to code after the original issue was fixed. They get forgotten, or people are wary of modifying working code. I'd wait, or at least leave the code *as it should be* in comments. As for the issue in hand, in EF6 this can happen if an entity's primary key *as EF knows it* itn't really unique. Not sure if this is still true in EF7. – Gert Arnold May 14 '16 at 23:18
  • Yes, that is very true. Thank you for the suggestion. I checked by defining PKs for both Customer and Postcode in EF (the tool generated code for some reason did not have them defined, despite them being defined in the database). But I am still getting the same issue as before. – rasika May 15 '16 at 21:31

0 Answers0