簡體   English   中英

將 IncludeFilter 與 EF6 一起使用時出現強制轉換異常

[英]Cast exception when using IncludeFilter with EF6

因此,我在嘗試運行 IncludeFilter 查詢時遇到了一些問題。 我的項目建立在 .NET 4.5.2 之上,我使用的是 EF6。

我需要的是能夠過濾結果,因為使用標准的 Include 方法只會返回大量數據。

我得到的錯誤如下:

{"Unable to cast object of type '<>f__AnonymousType0`2[SQL.Repository.ContractServices,System.Linq.IQueryable`1[System.Collections.Generic.List`1[SQL.Repository.ContractServicesInstallments]]]' to type 'SQL.Repository.ContractServices'."}

我的代碼如下:

public async Task<List<ContractServices>> GetAll(FilterDTO filterDTO, long companyID)
{
            
var listContracts = await context.ContractServices
                .IncludeFilter(a => a.ContractServicesInstallments.Where(b => b.DueDate 
                               >= DateTime.Today))
                .Where(a => a.companyID == companyID)
                .Skip(filterDTO.Skip)
                .Take(filterDTO.Take.Value)
                .ToListAsync();
                return listContracts;
}

如果我使用標准 .Include 運行它,一切正常。

試試下面的代碼。

public async Task<List<ContractServices>> GetAll(FilterDTO filterDTO, long companyID)
    {
                
    var listContracts = await context.ContractServices
                    .IncludeFilter(a => a.ContractServicesInstallments.Where(b => b.DueDate 
                                   >= DateTime.Today))
                    .IncludeFilter(a => a.ContractServicesInstallments.Where(a => a.companyID == companyID))
                    .Skip(filterDTO.Skip)
                    .Take(filterDTO.Take.Value)
                    .ToListAsync();
    
                    return listContracts;
    }`enter code here`

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM