我正在使用 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; }
}
PlayerComment
与Player
相连,而Player
又与SubGroup
相连,最后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();
}
然而,现在它会抛出运行时异常并给我以下错误信息:“无法将类型为'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子句时会出现问题。