我对使用 Entity Framework 还很陌生,并且一直遇到代码无法编译的问题。我收到的错误消息是:
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.
我做了一些搜索并发现了一些类似的问题,但没有任何东西可以帮助我解决我的问题。我只是想输出一些简单的内容以进行调试,这是我目前拥有的代码:
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);
如果您能帮助解决此错误,我将不胜感激
最佳答案
如Thomas Levesque's comment所示,用于连接集合的键必须是同一类型。因此,在您的情况下, user.Id
和 meeting.Guid
不是同一类型(int
和 Guid
, 分别)。您尚未提供有关这些对象的结构的信息,因此我无法准确告诉您如何更新代码。
DotNotPerls对 GroupJoin 的工作原理有一个很好的基本概述,并对参数进行了如下描述:
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.
关于c# - 无法从用法推断 GroupJoin 方法的类型参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26481982/