Why is this Linq not compiling?

In this simplified version of my real problem, I have two tables: User and Metadata . Each user can have a different number of metadata records associated with them through FKEY.

This Linq compiles fine:

 var user = from u in Context.Users join m in Context.Metadata on u.id equals m.userid select new { u.id, m.value } 

However, if I replaced the sentence string 'on' as follows:

 on new { u.id } equals new { m.userid } 

it does not compile with this error:

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

Does anyone know why?

And for bonus points:

I end up trying to execute a query like this:

 var user = from u in Context.Users join m in Context.Metadata on new { u.id, "mystring" } equals new { m.userid, m.key } select new { u.id, m.value } 

Note the use of the literal "mystring" . Needless to say, this does not work either.

Thanks!

EDIT: SLaks answer worked, but in order to fully understand what he was talking about, the final on clause that works is as follows:

 on new { id = u.id, key = "foo" } equals new { id = mu.userid, key = m.key } 
+4
source share
1 answer

Property names in your anonymous types must match.

You can specify the names as follows: new { UserId = u.id, Key = "mystring" }

+9
source

All Articles