c# - 无法从用法推断 GroupJoin 方法的类型参数

标签 c# linq entity-framework

我对使用 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.Idmeeting.Guid 不是同一类型(intGuid , 分别)。您尚未提供有关这些对象的结构的信息,因此我无法准确告诉您如何更新代码。

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/

相关文章:

c# - Entity Framework 中带有 OR 条件的动态查询

c# - 使用不同的通用参数实现相同的通用接口(interface) 2 次

c# - 编辑 DataGridView 中的整行(不仅仅是一个单元格)

c# - 使用 Newtonsoft.Json 解析 JSON 时出错

c# - "OR"按需使用 Linq to Entities,我该怎么做?

sql-server - Azure SQL 频繁连接超时

c# - 如何处理动态创建的控件?

c# - 如何从托管代码项目中调试 native 代码项目? C++/C#

c# - 两个值之间的距离

c# - 强制 Entity Framework 使用 datetime 而不是 datetime2