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

3

我正在使用 Entity Framework rc1-finalASP.NET5 中。

我有以下表格。

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; }

}

PlayerCommentPlayer相连,而Player又与SubGroup相连,最后SubGroupGroup相连。

我有以下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();
    }

然而,现在它会抛出运行时异常并给我以下错误信息:“无法将类型为'System.Linq.Expressions.FieldExpression'的对象强制转换为类型'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并使用3级深度的where子句时会出现问题。

1个回答

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();

1
谢谢。看起来更整洁了。我改成了它,但是它给了我相同的错误。 - dfmetro
1
嗨@devc2,看看我的更新,我尝试避免在您的查询中比较可空字段,只使用基本类型。 - ocuenca
1
谢谢你的帮助。我已经更新了我的原始帖子。似乎是EF7 RC的问题。 - dfmetro

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接