'Cast exception when using IncludeFilter with EF6
So, I'm facing some problems when trying to run an IncludeFilter query. My project is built upon .NET 4.5.2 and I'm using 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.
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.
Solution 1:[1]
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`
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Chamila Maddumage |
