无法将类型为“System.Linq.Expressions.FieldExpression”的对象强制转换为“System.Linq.Expressions.ParameterExpression

时间:2016-01-27 16:10:44

标签: entity-framework linq entity-framework-core

我在Entity Framework rc1-final

中使用ASP.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相关联,该SubGroupGroup相关联,并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(); }

我有以下LINQ查询

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

所以我的代码看起来像这样

OrderBy

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

  

“无法将'System.Linq.Expressions.FieldExpression'类型的对象强制转换为'System.Linq.Expressions.ParameterExpression'。”

我在github上看到 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; 存在错误但我甚至没有使用订单的问题。

我可以使用任何解决方法来修复此问题吗?

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

将我的代码更改为:

GroupId

除非我选择null并保留其他SubGroup,否则所有组合都有效。自Pattern p = Pattern.compile("(?:(\\d+)|([+-*/\\(\\)]))"); Matcher m = p.matcher("100+(0.03*55)/45-(25+55)"); List<String> tokens = new LinkedList<String>(); while(m.find()) { String token = m.group( 0 ); //group 0 is always the entire match tokens.add(token); } 起作用后,我只能推断出当使用Include并使用where where 3级深度时它是一个问题。

1 个答案:

答案 0 :(得分:3)

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

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