Включить фильтры для ядра Entity Framework - PullRequest
0 голосов
/ 19 января 2020

Я пытался найти информацию о фильтрации моих звонков в базу данных, чтобы переупорядочить данные из базы данных, и обнаружил, что пока нет способа отфильтровать .Inclunde или .ThenInclude.

Ниже я попробовал, есть ли способ сортировки коллекции?

var workout = await context.Workouts
                           //.Include(w => w.Segments.OrderByDescending(x => x.Id))
                           .Include(w => w.Segments)
                           .ThenInclude(s => s.Sets)     //this gets the sets for the segment
                           .Include(w => w.Segments)
                           .ThenInclude(s => s.Exercise)     //try intellisense didn't show exercise
                           .Include(w => w.BodyParts)       //to delete from bodyparts column need to fix
                           .SingleOrDefaultAsync(w => w.Id == id);


   .Include(w => w.Segments.OrderByDescending(x => x.Name))  //doesn't work

   .ThenInclude(s => s.Sets.OrderByDescending(x => x.Name))  //doesn't work 

Ниже код не может преобразовать заказ IEnumerable в мой домен

var workoutSegments = workout.Segments.OrderByDescending(x => x.Name);

foreach( var segment in workout.Segments)
{
    segment.Sets.OrderByDescending(x => x.Name).ToList();
}    

return workoutSegments;

public class Workout
{
    public int Id { get; set; }
    public string UserId { get; set; }
    public string Name { get; set; }

    public ICollection<Segment> Segments { get; set; }

    public Workout()
    {
        Segments = new Collection<Segment>();
    } 
}

1 Ответ

1 голос
/ 19 января 2020

Есть сторонняя библиотека, чтобы сделать заказ во включении. Я не могу вспомнить, какое имя. Но у меня есть другой способ.

var workout = await context.Workouts
     .Include(w => w.Segments)
        .ThenInclude(s => s.Sets)
    .Include(w => w.Segments)
        .ThenInclude(s => s.Exercise)
        .Include(w => w.BodyParts)
    .SingleOrDefaultAsync(w => w.Id == id);

workout.Segments = workout.Segments.OrderByDescending(x => x.Name);
foreach (var seg in workout.Segments)
{
    seg.Sets = seg.Sets.OrderByDescending(x => x.Name);
}
//Do same way for other includes.
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...