Is there a shorter, better and optimised way to fill this list from SQL database

Multi tool use
Is there a shorter, better and optimised way to fill this list from SQL database
I got this code and it works without problems. But i sense there is much better way to do this.
namespace Repositories
{
public class AuthorRepository : IAuthorRepository
{
public List<Author> GetAllFromRepo()
{
using (AppContext myDB = new AppContext())
{
List<Author> authorsFromRepo = new List<Author>();
foreach (var item in myDB.Authors)
{
authorsFromRepo.Add(new Author()
{
Books = new List<Book>(),
ID = item.ID,
FirstName = item.FirstName,
LastName = item.LastName
});
}
return authorsFromRepo.ToList();
}
}
}
}
When i try something along the lines of this:
public List<Author> GetAllFromRepo()
{
using (AppContext myDB = new AppContext())
{
List<Author> authorsFromRepo = new List<Author>();
authorsFromRepo = myDB.Authors.ToList();
return authorsFromRepo;
}
}
I always get this error:
Value cannot be null.
Parameter name: source
Exception Details: System.ArgumentNullException: Value cannot be null.
Parameter name: source
Source Error: Line 33: return authors.Select(x => new AuthorViewModel()
Any Help?
The model where the error takes me
namespace Services
{
public class AuthorService : IAuthorService
{
private readonly IAuthorRepository _AuthorRepository;
public AuthorService(IAuthorRepository authorRepository)
{
_AuthorRepository = authorRepository;
}
public List<AuthorViewModel> GetAll()
{
List<Author> authors = _AuthorRepository.GetAllFromRepo();
return authors.Select(x => new AuthorViewModel()
{
ID = x.ID,
FullName = $"{x.FirstName } {x.LastName} ",
Books = x.Books.Select(g => new BookViewModel()
{
ID = g.ID,
Name = g.Name
}).ToList()
}).ToList();
}
}
}
To add again, everything works fine if i use the first example of code.
When i try something shorter like
return myDB.Authors.ToList();
i get the error.
when i change to:
return authors.Select(x => new AuthorViewModel()
{
ID = x.ID,
FullName = $"{x.FirstName } {x.LastName} ",
Books = {}
}).ToList();
It works then... but this means it doesn't read the author books...
AuthorViewModel
return myDB.Authors.ToList();
I thought so also but i still have the same error. Here is the Model btw,
– Happy Coconut
Jul 1 at 23:56
Is
authors
in List<Author> authors = _AuthorRepository.GetAllFromRepo();
null
?– Stephen Muecke
Jul 2 at 0:06
authors
List<Author> authors = _AuthorRepository.GetAllFromRepo();
null
Or is
Books
in x.Books
null
?– Stephen Muecke
Jul 2 at 0:10
Books
x.Books
null
Because in the first code you are doing
Books = new List<Book>(),
and that's not there in second code. So you are getting error because x.Books
is null.– Chetan Ranpariya
Jul 2 at 0:17
Books = new List<Book>(),
x.Books
1 Answer
1
I had to change the model for Authors to
namespace Entities
{
public class Author
{
// this is what a had to add here
// From here
public Author()
{
Books = new List<Book>();
}
// to here
public int ID { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public List<Book> Books { get; set; }
}
}
So It wont give me any errors when x.Books is null.
HM! Check the specification of
Author.Books
, If your specification says that a NULL value means something different than "Author did not write any books", for instance: "don't know about his books", then you should not change it into: "this Author didn't write any books". Getting rid of your exception does not mean that you solved the error. If a NULL return is specified equal to an empty list, why do you ever allow that Author.Books equals null? In LINQ it is usually better not to return NULL when an IEnumerable is expected but return Enumerable.Empty<Book>()
– Harald Coppoolse
Jul 3 at 9:05
Author.Books
Enumerable.Empty<Book>()
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Your error refers to
AuthorViewModel
- what is that model? - all hat should be necessary is one line of code -return myDB.Authors.ToList();
– Stephen Muecke
Jul 1 at 23:52