I am trying to join two tables together using C# linq to entities.
One table has clients, the other table has licences. The tables are joined by Client_ID and ClientLicence_ClientID. For some reason (they are not my tables) the Client_ID is an int, and the ClientLicence_ClientID is a string.
The code I have is:
var licences = (from client in entities.Clients
join licence in entities.ClientLicences
on new { clientId = SqlFunctions.StringConvert((double)client.Client_ID) } equals
new { licence.ClientLicence_ClientID }
into clientGroup
where (filter.clientId == 0 || client.Client_ID == filter.clientId)
select licence);
When I try to compile this I get an error: The type of one of the expressions in the join clause is incorrect. Type inference failed in the call to ‘GroupJoin’.
If I comment out the where statement then it works (albeit without filtering results).
The filter variable is a class that is passed in to the function. It only has one propery: int clientId.
Look at your join:
The key types are:
clientIdClientLicence_ClientIDThose two anonymous types can’t be compared for equality. I suspect you don’t need anonymous types at all here, actually. I’d expect this to work:
(Obviously you don’t need all the new lines – I was just trying to separate out the various bits for clarity.)
You claim that without the
whereclause it works – but that would be very surprising, given that it’s the join that’s a problem.