entity-framework - 无法将类型为 'System.Linq.Expressions.FieldExpression' 的对象转换为类型“System.Linq.Expressions.ParameterExpression

标签 entity-framework linq entity-framework-core

我在 ASP.NET5 中使用 Entity Framework rc1-final

我有下表。

public class PlayerComment 
{
    [Key]
    public int Id { get; set; }

    public int? PeriodId { get; set; }

    [ForeignKey("PeriodId")]
    public Period Period { get; set; }

    public int? PlayerId { get; set; }
    [ForeignKey("PlayerId")]
    public Player Player { get; set; 
    public DateTime? CommentDate { get; set; }

    public string Comment { get; set; }

}

PlayerComment 链接到 Player,后者链接到 SubGroup,后者链接到 Group

我有以下 LINQ 查询

public async Task<IEnumerable<PlayerComment>>  SearchQueryable(int? groupId, int? subGroupId = null, int? playerId = null)
    {

        var table = (from pc in _db.PlayerComments
                     join p in _db.Players on pc.PlayerId equals p.Id
                     join sg in _db.SubGroups on p.SubGroupId equals sg.Id
                     where (sg.GroupId == groupId || groupId == null)
                         &&
                         (p.SubGroupId == subGroupId || subGroupId == null)
                           &&
                         (p.Id == playerId || playerId == null)
                     select pc);
        return table.ToListAsync();
    }

这可以正常工作。

每个评论都属于一个句点,所以在我的输出中我需要包含句点,所以我添加了 .Include("Period")

所以我的代码是这样的

public async Task<IEnumerable<PlayerComment>>  SearchQueryable(int? groupId, int? subGroupId = null, int? playerId = null)
    {

        var table = (from pc in _db.PlayerComments
                     join p in _db.Players on pc.PlayerId equals p.Id
                     join sg in _db.SubGroups on p.SubGroupId equals sg.Id
                     where (sg.GroupId == groupId || groupId == null)
                         &&
                         (p.SubGroupId == subGroupId || subGroupId == null)
                           &&
                         (p.Id == playerId || playerId == null)
                     select pc).Include(p => p.Period);
        return table.ToListAsync();
    }

但是现在它抛出一个运行时异常并给我:

"Unable to cast object of type 'System.Linq.Expressions.FieldExpression' to type 'System.Linq.Expressions.ParameterExpression'."

我阅读了github OrderBy 出现问题,但我什至没有使用 order by。

有什么解决方法可以解决这个问题吗?

由于@octavioccl 提供的答案,我似乎已经缩小了范围。

将我的代码更改为:

        var table = _db.PlayerComments.Include(q => q.Period)
                      .Include(sg => sg.Player.SubGroup);
        IQueryable<PlayerComment> tableFiltered;
        if (playerId != null)
        {
            tableFiltered = table.Where(p => p.Player.Id == playerId)
        }
        else
        {
            if (subGroupId != null)
            {
                tableFiltered = table.Where(p => p.Player.SubGroupId == subGroupId)
            }
            else
            {
                if (groupId != null)
                {
                    tableFiltered = table.Where(p => p.Player.SubGroup.GroupId == groupId)
                }
                else
                {
                    tableFiltered = table
                }
            }

        }
        return tableFiltered;

所有组合都有效,除非我选择 GroupId 并将其他组合保持为 null。由于 SubGroup 有效,我只能推断当您使用 Include 并使用 where 子句 3 层深度时这是一个问题。

最佳答案

您应该尝试在要加载相关实体的 DbSet 中调用 Include 方法:

 var table = (from pc in _db.PlayerComments.Include(p => p.Period)
              //...

而且我认为如果您使用导航属性而不是显式连接,您的查询会更简单:

var table =await _db.PlayerComments.Include(p => p.Period)
                                   .Include(p => p.Player.SubGroup.Group)
                                   .Where(pc=>  ( pc.Player.SubGroup.Group.GroupId == groupId || groupId == null) 
                                             && ( pc.Player.SubGroup.SubGroupId == subGroupId || subGroupId == null)
                                             && ( pc.Player.Id == playerId || playerId == null))
                                   .ToListAsync();

更新

尝试将检查参数是否为 null 的条件移到查询之外。

bool groupIdIsNull=groupId == null;
bool subGroupIdIsNull=subGroupId == null;
bool playerIdIsNull= playerId==null;

var table =await _db.PlayerComments.Include(p => p.Period)
                                   .Include(p => p.Player.SubGroup.Group)
                                   .Where(pc=>  ( groupIdIsNull || pc.Player.SubGroup.Group.GroupId.Value == groupId.Value) 
                                             && ( subGroupIdIsNull || pc.Player.SubGroup.SubGroupId.Value == subGroupId.Value )
                                             && ( playerIdIsNull || pc.Player.Id.Value == playerId.Value))
                                   .ToListAsync();

关于entity-framework - 无法将类型为 'System.Linq.Expressions.FieldExpression' 的对象转换为类型“System.Linq.Expressions.ParameterExpression,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35042379/

相关文章:

c# - 在 Code First EF 中声明 List 而不是 ICollection 有何效果?

linq - 使用(Linq for JavaScript 库)子查询(操作方法)

asp.net-mvc - EF 恢复的对象中的空属性

c# - EF,如何提高查询性能

c# - 带有 ODP.Net Oracle.ManagedDataAccess 的 EF 6,如何对类属性使用非大写字母?

c# - 如何从分组但仍按标题顺序返回表中的数据?

c# - 带有两个 where 子句的 Linq 语句

linq-to-entities - 删除实体而不将它们加载到内存中

entity-framework-core - EF 核心 : updating an entity without querying it first

entity-framework-core - `groupjoin` 的查询无法翻译,尽管它被证明是受支持的