Ho creato un metodo che filtra il mio set di dati. Ho anche creato un altro metodo che conta la quantità di articoli che ci sono con i filtri applicati. Questo metodo assomiglia a questo:
return new Count
{
Address = this.Filter(/* paramters to filter */)
.GroupBy(person => person.Address.City)
.Select(group => new Amount<string>
{
Type = group.Key /* so the city */,
Count = group.Count()
}),
};
Count ha un paio di elenchi di oggetti importo. La quantità di oggetti contiene il nome di ciò che verrà filtrato e un conteggio di quanti filtri contiene.
this.Filter()
È un metodo privato che restituirà un IQueryable da DbContext.
Tutto funziona, tuttavia, è molto lento con una grande quantità di dati. Il motivo è che GroupBy () e Count () non possono essere tradotti e saranno valutati localmente. Fornisce i seguenti avvertimenti:
warn: Microsoft.EntityFrameworkCore.Query[20500]
The LINQ expression 'GroupBy([entity.RequestRating], [entity])' could not be translated and will be evaluated locally.
warn: Microsoft.EntityFrameworkCore.Query[20500]
The LINQ expression 'Count()' could not be translated and will be evaluated locally.
warn: Microsoft.EntityFrameworkCore.Query[20500]
The LINQ expression 'Count()' could not be translated and will be evaluated locally.
Quale sarebbe un approccio migliore a questo? Questo è un progetto ASP.NET CORE
Prova questo:
var amountList = Persons.Where(paramters to filter)
.GroupBy(person => person.Address.City)
.Select(group => new Amount()
{
City = group.Key,
CityCount = group.Count()
}).Tolist();