3
votes

I'm quite new to using Entity Framework and I keep running into this problem where my code can't compile. The error message I get is:

The type arguments for method 'System.Linq.Queryable.GroupJoin(System.Linq.IQueryable, System.Collections.Generic.IEnumerable, System.Linq.Expressions.Expression>, System.Linq.Expressions.Expression>, System.Linq.Expressions.Expression,TResult>>)' cannot be inferred from the usage. Try specifying the type arguments explicitly.

I've done some search and found some similar problems but nothing has helped me fix my problem. I'm just trying to output something simple for debugging purposes here is the code I have at the moment:

var result = _context.Set<User>()
             .Where(user => user.GraphMeetingStatistics && user.Id == userId)
             .GroupJoin(_context.Set<Meeting>()
                 .Where(meeting => meeting.UserId == userId),
                 user => user.Id,
                 meeting => meeting.Guid,
                 (user, meeting) => meeting);

I'd appreciate any help with this error

1
Are user.Id and meeting.Guid of the same type? - Thomas Levesque
No user.Id is an int and meeting.Guid is a Guid - Serberuss
Well, that's your problem. For a join (or a group join), the ids must have the same type, since they must be matched. If you just want the meetings for a specific user, you don't need a join, just get the meetings where meeting.UserId == userId - Thomas Levesque
What are you trying to achieve? I think you have to join on user.Id and meeting.UserId. - Gert Arnold
Thomas' comment helped me fix it in the end. I ended up writing it a little differently but I kind of rethinked it and it's working fine now. Thanks for the help - Serberuss

1 Answers

7
votes

As indicated by Thomas Levesque's comment, the keys used to join the collections must be of the same type. So, in your case, user.Id and meeting.Guid are not the same type (int and Guid, respectively). You haven't provided information about the structure of these objects, so I can't tell you exactly how to update your code.

DotNotPerls has a good, basic overview of of how GroupJoin works, and describes the arguments as such:

Arguments 1 and 2:

Argument one is the secondary collection. And argument two is a Func that returns the key from the first object type.

Arguments 3 and 4:

A Func that returns the key from the second object type, and one that stores the grouped object with the group itself.