0

I have a class called Books, I am creating a list and populating it as follows

List<Books> booklist = new List<Books>();

foreach (BooksUsers bu in userbooks)
{
    Books book = _context.Books.Where(b => b.BookId == bu.BookId).Where(b => b.Void == false).FirstOrDefault();
    booklist.Add(book);
}
          

I want to sort the list by BookTitle, and am getting null reference exceptions on the lambda

I tired this

booklist.Sort((x, y) => x.BookTitle.CompareTo(y.BookTitle));

But get this

Object reference not set to an instance of an object. x was null.

I tried this

List<Books> SortedList = booklist.OrderBy(o => o.BookTitle).ToList();

Object reference not set to an instance of an object. o was null.

and this

booklist.OrderBy(bl => bl.BookTitle).ToList();

Object reference not set to an instance of an object. bl was null.

booklist has items.

Can anyone shed some light?

dymanoid
  • 13,597
  • 3
  • 34
  • 59

0 Answers0