Согласно документации EF Core, я мог бы добавить Where, OrderBy, OrderByDescending, thenBy, thenByDescending, Пропустить или выполнить операцию с навигационной коллекцией.
У меня есть этот код:
Код: Выделить всё
public async Task GetCategories(int recipeCount)
{
var categories = _context.Categories
.Include(cat => cat.Recipes.Take(recipeCount));
var dtos = await categories
.Select(cat => _mapper.Map(cat))
.ToListAsync();
return dtos;
}
Код: Выделить всё
public static IEnumerable TakeMaybe(this IEnumerable enumerable, int? count)
{
if (count.HasValue)
return enumerable.Take(count.Value);
return enumerable;
}
// And changing GetCategories as follows:
public async Task GetCategories(int? recipeCount)
{
var categories = _context.Categories
.Include(cat => cat.Recipes.TakeMaybe(recipeCount));
var dtos = await categories
.Select(cat => _mapper.Map(cat))
.ToListAsync();
return dtos;
}
System.InvalidOperationException:
Выражение 'cat.Recetas.TakeMaybe(__queryParameters_CantRecetas_0)' недействителен внутри операции 'Include', поскольку он не представляет доступ к свойству: 't => t.MyProperty'.
Для целевой навигации, объявленной на производные типы, используйте приведение ('t => ((Derived)t).MyProperty') или оператор 'as' ('t => (t as Derived).MyProperty').
Collection доступ к навигации можно отфильтровать, составив операции Where, OrderBy(Descending), thenBy(Descending), Skip или Take.
Для получения дополнительной информации о включении связанных данных см. https://go.microsoft.com/fwlink/? LinkID=746393.
Я не уверен, почему это происходит, поскольку я либо вызываю оператор Take внутри метода, либо просто возвращаю перечисляемое как есть.
В документации прямо указано, что единственными разрешенными методами являются Where, OrderBy, OrderByDescending, thenBy, thenByDescending, Skip и Take, но я не совсем понимаю, почему я не могу сделать то, что пытаюсь сделать. Я думаю, что это может быть как-то связано с магией перевода запросов, которую делает EF Core, но я не совсем ее понимаю, поэтому не уверен.
Я мог бы просто построить запрос условно, по частям:
Код: Выделить всё
public async Task GetCategories(int? recipeCount)
{
var categories = _context.Categories;
if (recipeCount.HasValue)
categories = categories.Include(cat => cat.Recipes.Take(recipeCount.Value));
else
categories = categories.Include(cat => cat.Recipes);
var dtos = await categories
.Select(cat => _mapper.Map(cat))
.ToListAsync();
return dtos;
}
Код: Выделить всё
public static IEnumerable Paginate(this IEnumerable enumerable, int? offset, int? length)
{
if (offset.HasValue)
enumerable = enumerable.Skip(offset.Value);
if (length.HasValue)
enumerable = enumerable.Take(length.Value);
return enumerable;
}
public static IEnumerable Order(this IEnumerable enumerable, Func keySelector, Ordering order)
{
return order switch
{
Ordering.Ascending => enumerable.OrderBy(keySelector),
Ordering.Descending => enumerable.OrderByDescending(keySelector),
_ => enumerable
};
}
А как еще можно реализовать подобное?
Спасибо!
Подробнее здесь: https://stackoverflow.com/questions/789 ... ds-ef-core
Мобильная версия