gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 08:29:13 26 4
gpt4 key购买 nike

我在 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com