1

I got two lists that I need to join to set a value between them. The first list is companytaxesnumber and the second is ordertaxes. Both of them got a TaxId which is used to join them.

Here the code about that :

destination.OrderTaxes = destination.OrderTaxes.
            Join(src.companytaxesnumber, vModel => vModel.TaxId, source => source.TaxId, (dest, source) =>
            {
                  dest.LegalNumber = source.LegalNumber; return dest;
            }).ToList();

My problem is if companytaxesnumber have less data then ordertaxes (for any reason), the join operator will return a result with the count of companytaxesnumber and will lost the other data of ordertaxes that it has not been able to join.

Did you know how can I make sure I got the complete ordertaxes list even if some data has not been join with companytaxesnumber?

Ousmane D.
  • 54,915
  • 8
  • 91
  • 126
David Létourneau
  • 1,250
  • 2
  • 19
  • 39

1 Answers1

2

You're looking for a LEFT OUTER JOIN, you can accomplish your task with:

var result = from o in destination.OrderTaxes
             join d in src.companytaxesnumber on o.TaxId equals d.TaxId into temp
             from p in temp.DefaultIfEmpty()
             select new { /* assign the fields appropriately */ };

a LEFT OUTER JOIN will allow you to retain all elements on the first collection regardless of whether it has any correlated elements in the second collection.

Ousmane D.
  • 54,915
  • 8
  • 91
  • 126
  • Is there any equivalent with the same syntax of the code I showed? – David Létourneau Oct 29 '17 at 19:10
  • 1
    you can but it gets a little bit messy than it's using query syntax. see [**How do you perform a left outer join using linq extension methods**](https://stackoverflow.com/questions/584820/how-do-you-perform-a-left-outer-join-using-linq-extension-methods) – Ousmane D. Oct 29 '17 at 19:12