無法將類型為“System.Linq.Expressions.FieldExpression”的對象強制轉換為“System.Linq.Expressions.ParameterExpression”

entity-framework entity-framework-core linq

我在ASP.NET5.使用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 ,鏈接到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並使用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();



許可下: CC-BY-SA with attribution
不隸屬於 Stack Overflow
這個KB合法嗎? 是的,了解原因
許可下: CC-BY-SA with attribution
不隸屬於 Stack Overflow
這個KB合法嗎? 是的,了解原因