Linq Join when using EF7

c# entity-framework entity-framework-core linq

Question

I have the following linq statement:

( from apiKey in context.Set<ApiKey>()
                    join application in context.Set<Application>() on apiKey.User equals application.User
                    join paymentGateway in context.Set<PaymentGateway>() on apiKey.User equals paymentGateway.User
                    where apiKey.Key == secureRequest.ApiKey
                    where application.Secret == secureRequest.ApplicationSecret
                    where paymentGateway.Id == secureRequest.PaymentGatewayId
                    select apiKey ).AnyAsync();

But which as far as I know is correct but when I run the code I get the following error.

Expression of type 'System.Collections.Generic.IEnumerable`1[System.Data.Common.DbDataReader]' cannot be used for parameter of type 'System.Collections.Generic.IEnumerable`1[Microsoft.Data.Entity.Query.QuerySourceScope`1[DomainModel.PaymentGateway]]' of method 'System.Collections.Generic.IEnumerable`1[Microsoft.Data.Entity.Query.QuerySourceScope] _Join[QuerySourceScope,QuerySourceScope`1,User,QuerySourceScope](System.Collections.Generic.IEnumerable`1[Microsoft.Data.Entity.Query.QuerySourceScope], System.Collections.Generic.IEnumerable`1[Microsoft.Data.Entity.Query.QuerySourceScope`1[DomainModel.PaymentGateway]], System.Func`2[Microsoft.Data.Entity.Query.QuerySourceScope,PaymentGatewayWrapper.DomainModel.User], System.Func`2[Microsoft.Data.Entity.Query.QuerySourceScope`1[PaymentGatewayWrapper.DomainModel.PaymentGateway],PaymentGatewayWrapper.DomainModel.User], System.Func`3[Microsoft.Data.Entity.Query.QuerySourceScope,Microsoft.Data.Entity.Query.QuerySourceScope`1[DomainModel.PaymentGateway],Microsoft.Data.Entity.Query.QuerySourceScope])'

Any pointer in the right direction would be great thanks.

After some investigatio I have found that it is the Any that is causing this error. So with the following:

( from apiKey in context.Set<ApiKey>()
                        join application in context.Set<Application>() on apiKey.User equals application.User
                        join paymentGateway in context.Set<PaymentGateway>() on apiKey.User equals paymentGateway.User
                        where apiKey.Key == secureRequest.ApiKey
                        where application.Secret == secureRequest.ApplicationSecret
                        where paymentGateway.Id == secureRequest.PaymentGatewayId
                        select apiKey ).ToListAsync();

I get this error:

The multi-part identifier "application.Secret" could not be bound.
The multi-part identifier "paymentGateway.Id" could not be bound.

If anyone knows what this problem is, but I don't think EF 7 supports join properly yet

Popular Answer

As I know the Join should be on primitive types. in your join it's appear that you do join for object User but you should do the join for UserId instead.

( from apiKey in context.Set<ApiKey>()
                    join application in context.Set<Application>() on apiKey.UserId equals application.UserId
                    join paymentGateway in context.Set<PaymentGateway>() on apiKey.UserId equals paymentGateway.UserId
                    where apiKey.Key == secureRequest.ApiKey
                    where application.Secret == secureRequest.ApplicationSecret
                    where paymentGateway.Id == secureRequest.PaymentGatewayId
                    select apiKey ).AnyAsync();



Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why