5

I have a peculiar problem with LINQ to SQL:

Doing this is fine:

from s in Something
join a in AnotherThing
on s.NullableDateTime.Value
equals a.DateTime
select s

However, using anonymous type like so:

from s in Something
join a in AnotherThing
on new { s.NullableDateTime.Value }
equals new { a.DateTime }
select s

Results in

The type of one of the expressions in the join clause is incorrect. Type inference failed in the call to 'Join'.

I need to use anonymous types as I aim to add another column to join on.

Any ideas as to why this is occurring and how to fix?

dav_i
  • 27,509
  • 17
  • 104
  • 136

1 Answers1

13

You should tell to compiler, what properties it must compare:

 on new { s.NullableDateTime.Value }
 equals new { Value = a.DateTime }

The first creates an anonymous type, which looks like this:

class A
{
    public DateTime Value { get; set; }
}

The second line in your sample creates another anonymous type:

class B
{
    public DateTime DateTime { get; set; }
}

Hence, compiler can't understand, that you want to compare a.Value with b.DateTime.

Dennis
  • 37,026
  • 10
  • 82
  • 150
  • Ohhhhhh I understand now. I was making some incorrect assumptions. Thanks for clearing this up! – dav_i Oct 02 '13 at 11:03