Hey everyone i have a working LINQ query but i was wondering if it could be optimized even more..
this is what i'm doing right now:
var selectionsAnimals = await this.context.SelectionAnimals
.GroupBy(a => a.AnimalId)
.ToListAsync();
var animalsSelections = selectionsAnimals
.Select(a => new AnimalsSelection
{
AnimalId = a.First().AnimalId,
SelectionIds = a.Select(b => b.SelectionId)
.OrderBy(b => b)
.ToList()
})
.ToList();
I was wondering if these two LINQ statements could be merged and as one awaitable LINQ query to the database?
This is the Database entity:
public class SelectionAnimals
{
public int Id { get; set; }
public int AnimalId { get; set; }
public int SelectionId { get; set; }
public DateTime CreatedAt { get; set; }
public DateTime UpdatedAt { get; set; }
}
This is the model:
public class AnimalsSelection
{
public int AnimalId { get; set; }
public IList<int> SelectionIds { get; set; }
}
in the end i'm mapping animalsSelection to a IEnumerable<AnimalsSelection>
(the model)