我一直在尝试查找有关过滤对数据库的调用以对数据库中的数据进行重新排序的信息,并且发现它们尚无法过滤.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>();
}
}
答案 0 :(得分:1)
在include中有第3方库可以进行订购。我不记得叫什么名字了。但是我有另一种方式。
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.