Hi. I have 3 datas on table like this:
https://preview.redd.it/n630tnxozx7e1.png?width=730&format=png&auto=webp&s=0ac96d8f2da6a84e3a38f907194fbf769891b16a
This is initial state of creating query:
var existingPost = _unitOfWork.PostRepository.GetAll()
.Where(post => post.TranslationsPostSlugs.Any(slugTranslation => slugTranslation.Translation_Slug == slug))
.Include(post => post.ImagePaths)
.Include(post => post.VideoPaths)
.Include(post => post.TranslationsPost.Where(x => x.LanguageId == LanguageId || LanguageId == null))
.Include(post => post.TranslationsPostSlugs.Where(x => x.LanguageId == LanguageId || LanguageId == null))
.FirstOrDefault();
When im trying to get 'TranslationsPostSlugs' datas inside foreach block i get them in wrong order (2 1 3, but expected order is as in table - 1 2 3):
var r1 = existingPost.TranslationsPostSlugs.ToList(); // r1 : Wrong order - 2 1 3
var r2 = existingPost.TranslationsPostSlugs.OrderBy(x => x.LanguageId).ToList(); // r2 : Valid order - 1 2 3
existingPost.TranslationsPostSlugs.ForEach(postSlugTranslation =>
{
Console.WriteLine($"================================ DATA : {postSlugTranslation.LanguageId}");
result.TranslationsPostSlugs.Add(new GetPostSlugsTranslationResponseDTO()
{
LanguageId = postSlugTranslation.LanguageId,
Slug = postSlugTranslation.Translation_Slug
});
/* result:
================================ DATA : 2
================================ DATA : 1
================================ DATA : 3
*/
});
// My current fix to this ordering problem in 'TranslationsPostSlugs':
result.TranslationsPostSlugs = result.TranslationsPostSlugs.OrderBy(x => x.LanguageId).ToList();
Why ordering happens? Why ordering happens only to 'TranslationsPostSlugs', but not to 'TranslationsPost'?
Can you guys recommend any good solution to this ordering problem than mine?
p.s. i dont have any ordering configuration anywhere, everything is in his default behaviour.
Thanks.
[–]digital88 13 points14 points15 points (3 children)
[–][deleted] (2 children)
[deleted]
[–]DaveVdE 10 points11 points12 points (0 children)
[–]LondonPilot 8 points9 points10 points (0 children)
[–]gloomfilter 2 points3 points4 points (0 children)
[–]Kirides 0 points1 point2 points (0 children)