var articles = context.Articles.Where(a => a.Id != articleId)
.OrderBy(p => p.Categories.OrderBy(q => q.Name).FirstOrDefault().Name).ToList();
i get message of possible NullReferenceException which is correct.
So I make
var  articles = context.Articles.Where(a => a.Id != articleId)
                               .OrderBy(p =>
                                   (p.Categories.OrderBy(q => q.Name).FirstOrDefault() != null
                                    ? p.Categories.OrderBy(q => q.Name).FirstOrDefault().Name
                                    : null))
                               .Skip(page * pageSize)
                                  .Take(pageSize)
                                  .ToList();
which works but statement is calling two times and can be slow so i try to make
var articles = context.Articles.Where(a => a.Id != articleId)
             .OrderBy(p =>
             {
                 var firstOrDefault = p.Categories.OrderBy(q => q.Name).FirstOrDefault();
                 return firstOrDefault != null ? firstOrDefault.Name : null;
             }).ToList();
but i get
lambda expression with a statement body can not be converted to an expression tree.
What can i do? Ss first example correct even if i call two times p.Categories.OrderBy(q => q.Name).FirstOrDefault().
I am thinking that this can be slow. I have 200k rows in database.
 
     
     
     
     
    