Using .Net Core 2.1 and EF Core 2.1.1 and SQL Server
I am trying to pull a list of Organizations
and their list of Communications
I then want to limit it to those that have not had any Communications
in the last 6 months
Here are my skimmed down ViewModels:
public class OrganizationViewModel
{
public Guid Id { get; set; }
public IEnumerable<CommunicationViewModel> CommunicationViewModels { get; set;
}
public class CommunicationViewModel
{
public Guid Id { get; set; }
public DateTime Date { get; set; }
public Guid OrganizationViewModelId { get; set; }
public OrganizationViewModel OrganizationViewModel { get; set; }
}
And here is my query:
DateTime sixMonthsAgo = DateTime.Today.AddMonths(-6);
int pageIndex = 1; // Would be passed in
int pageSize = 3;
IQueryable<OrganizationViewModel> query = _context.Organizations
.AsNoTracking()
.Select(organization => new OrganizationViewModel
{
CommunicationViewModels = organization.Communications.Select(communication => new CommunicationViewModel
{
Date = communication.Date
})
.OrderByDescending(communication => communication.Date)
.Take(1)
.ToList()
})
.Where(organization =>
(!searchViewModel.LimitToLastSixMonths ||
organization.CommunicationViewModels.Any(communication => communication.Date <= sixMonthsAgo)));
int totalAmount = await query.CountAsync();
List<OrganizationViewModel> items = await query
.Skip((pageIndex - 1) * pageSize)
.Take(pageSize)
.ToListAsync();
This gets me the expected results, but I can see in my logs that I'm performing this query on every record when I hit .CountAsync()
and . Skip(..).Take(..)
:
SELECT CASE
WHEN EXISTS (
SELECT 1
FROM (
SELECT TOP(1) [comm].[Date]
FROM [Communications] AS [comm]
WHERE @_outer_Id = [comm].[OrganizationId]
ORDER BY [comm].[Date] DESC
) AS [t]
WHERE [t].[Date] <= @__sixMonthsAgo_0)
THEN CAST(1 AS BIT) ELSE CAST(0 AS BIT)
END
I'm also seeing these warnings when .CountAsync()
is called (slightly edited):
Microsoft.EntityFrameworkCore.Query:Warning: The LINQ expression
'where (False OrElse {from CommunicationViewModel cvm in {from Communication comm in value(..EntityQueryable'1[..Models.Communication]) orderby [comm].Date desc where ?= (Property([o], "Id") == Property([comm], "OrganizationId")) =? select new CommunicationViewModel() {Date = [comm].Date} => Take(1) => AsQueryable()} where ([cvm].Date <= __sixMonthsAgo_0) select [cvm] => Any()})'
could not be translated and will be evaluated locally.
Microsoft.EntityFrameworkCore.Query:Warning: The LINQ expression 'Count()' could not be translated and will be evaluated locally.
And similar errors when the .Take(..).Skip(..)
is called:
Microsoft.EntityFrameworkCore.Query:Warning: The LINQ expression
same as above
could not be translated and will be evaluated locally. Microsoft.EntityFrameworkCore.Query:Warning: The LINQ expression 'Skip(__p_1)' could not be translated and will be evaluated locally.
Microsoft.EntityFrameworkCore.Query:Warning: The LINQ expression 'Take(__p_2)' could not be translated and will be evaluated locally.
This does not happen when searchViewModel.LimitToLastSixMonths
is false
Any suggestions on how I can rewrite my query to not perform that query locally on each record?
What if you try to include the navigation property into the select:
IQueryable<OrganizationViewModel> query = _context.Organizations
.Include(o => o.CommunicationViewModels)
...
I figured it out!
This is my new query:
DateTime sixMonthsAgo = DateTime.Today.AddMonths(-6);
int pageIndex = 1; // Would be passed in
int pageSize = 3;
IQueryable<OrganizationViewModel> query = _context.Organizations
.AsNoTracking()
.Select(organization => new OrganizationViewModel
{
CommunicationViewModels = organization.Communications.Select(communication =>
new CommunicationViewModel
{
Id = communication.Id,
Date = communication.Date
})
.OrderByDescending(communicationViewModel => communicationViewModel.Date)
.Take(1)
.Where(communicationViewModel => communicationViewModel.Date <= sixMonthsAgo)
.AsQueryable()
})
.Where(organizationViewModel =>
(!searchViewModel.LimitToLastSixMonths || organizationViewModel.CommunicationViewModels.Any()));
int totalAmount = await query.CountAsync();
List<OrganizationViewModel> items = await query
.Skip((pageIndex - 1) * pageSize)
.Take(pageSize)
.ToListAsync();
Which now produces these two queries when searchViewModel.LimitToLastSizeMonths
is true
:
SELECT COUNT(*)
FROM [Organizations] AS [organization]
WHERE EXISTS (
SELECT 1
FROM (
SELECT [t].[Id], [t].[Date]
FROM (
SELECT TOP(1) [communication].[Id], [communication].[Date]
FROM [Communications] AS [communication]
WHERE [organization].[Id] = [communication].[OrganizationId]
ORDER BY [communication].[Date] DESC
) AS [t]
WHERE [t].[Date] <= @__sixMonthsAgo_0
) AS [t0])
SELECT [organization].[Id]
FROM [Organizations] AS [organization]
WHERE EXISTS (
SELECT 1
FROM (
SELECT [t].[Id], [t].[Date]
FROM (
SELECT TOP(1) [communication].[Id], [communication].[Date]
FROM [Communications] AS [communication]
WHERE [organization].[Id] = [communication].[OrganizationId]
ORDER BY [communication].[Date] DESC
) AS [t]
WHERE [t].[Date] <= @__sixMonthsAgo_0
) AS [t0])
ORDER BY (SELECT 1)
OFFSET @__p_1 ROWS FETCH NEXT @__p_2 ROWS ONLY
Without the .AsQueryable()
it goes back to checking each record and performing the count and ski/take locally.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.