0

So I have the following Linq query:

var comb1 = (from m1 in _modified
                     join o1 in _original on m1.custom_field_option_id equals o1.custom_field_option_id
                     from e1 in _existing
                     .Where(row => (m1.custom_field_option_id == row.custom_field_option_id || m1.custom_field_option_id == 0)
                         && row.custom_field_id == m1.custom_field_id).DefaultIfEmpty()
                     select new { m1, o1, e1 }).ToArray();

In List _modifed I have two items. In List _original I have one item. The second item in _modified is a new insert record. _original does not contain this record and nor does _existing.

The new record is not being included in my combined array. The custom_field_option_id of the new record is currently 0 so I added to my where clause:

|| m1.custom_field_option_id == 0

But that didn't help.

How do I change this query to include the new record in _modified?

Brian Ogden
  • 18,439
  • 10
  • 97
  • 176
  • Should `_mod.custom_field_id` be m1.custom_field_id`, or am I missing something? – Rufus L Sep 16 '14 at 17:21
  • I changed _mod.custom_field_id to m1.custom_field_id and updated my answer. Still having the exact same issue, thanks for the help though! – Brian Ogden Sep 16 '14 at 17:28
  • Also, what is the end result that you're looking for? It would make it easier to try to help with an answer. – Rufus L Sep 16 '14 at 18:00
  • Well I am trying to include the 2nd record in _modified that is currently being excluded is really the goal. My end result would contain two records where it currently only contains one. – Brian Ogden Sep 16 '14 at 18:10
  • @BrianOgden have you tried left outer join like in this [question](http://stackoverflow.com/questions/3404975/left-outer-join-in-linq)? – aleha_84 Sep 16 '14 at 19:19

1 Answers1

1

unless i am missing sommething: join o1 in _original on m1.custom_field_option_id equals o1.custom_field_option_id is a hard join to whatever is in _original.

Could it be that the row is not present because the row is not in _original?

user3097514
  • 319
  • 2
  • 6
  • That seems the case, I thought I was taking advantage of .DefaultIfEmpty() properly to create a left outer join – Brian Ogden Sep 16 '14 at 17:40
  • Let's see if we can make this outer from m1 in _modified join o1 in _original on m1.custom_field_option_id equals o1.custom_field_option_id from m1 in _modifiled o1 in _original.Where(row => row.custom_field_option_id == m1.custom_field_option_id).DefaultIfEmpty() Conversely, just add a dummy row to _original for all missing rows. Ether way you will get your join. – user3097514 Sep 16 '14 at 18:23
  • Let's see if we can make this outer from m1 in _modified join o1 in _original on m1.custom_field_option_id equals o1.custom_field_option_id to from m1 in _modifiled o1 in _original.Where(row => row.custom_field_option_id == m1.custom_field_option_id).DefaultIfEmpty() OR Conversely, just add a dummy row to _original for all missing rows. Ether way you will get your join. – user3097514 Sep 16 '14 at 18:29
  • remove the || m1.custom_field_option_id == 0 – user3097514 Sep 16 '14 at 19:29