简体   繁体   English

将 IncludeFilter 与 EF6 一起使用时出现强制转换异常

[英]Cast exception when using IncludeFilter with EF6

So, I'm facing some problems when trying to run an IncludeFilter query.因此,我在尝试运行 IncludeFilter 查询时遇到了一些问题。 My project is built upon .NET 4.5.2 and I'm using EF6.我的项目建立在 .NET 4.5.2 之上,我使用的是 EF6。

What I need is to be able to filter the results because using the standard Include method would just return a huge amount of data.我需要的是能够过滤结果,因为使用标准的 Include 方法只会返回大量数据。

The error I'm getting is the following:我得到的错误如下:

{"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'."}

My code is the following:我的代码如下:

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;
}

If I run this with the standard .Include, everything works fine.如果我使用标准 .Include 运行它,一切正常。

Try the following code.试试下面的代码。

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