I've trying to query some results to fill a list of objects. When I do it using a foreach, it works, but when I try using linq lambda expression, throws a null refference. Also I only don't use the foreach code because take so much time.
Some know what is wrong?
Foreach code:
var aux = _myRepository.GetAll();
var processGames = new List<ProcessGamePersonDTO>();
foreach (var item in aux)
{
var processGame = new ProcessGamePersonDTO
{
IdProcessList = item.ProcessPersonList != null && item.ProcessPersonList.Any()
? item.ProcessPersonList.Select(x => x.Process.Id).ToList()
: new List<int>(),
IdGame = item.Game.Id,
GameName = item.Game.Name,
};
processGames.Add(processGame);
}
linq lambda code:
var aux = _myRepository.GetAll();
var processGames = aux.Select(item => new ProcessGamePersonDTO
{
IdProcessList = item.ProcessPersonList != null && item.ProcessPersonList.Any()
? item.ProcessPersonList.Select(x => x.Process.Id).ToList()
: new List<int>(),
IdGame = item.Game.Id,
GameName = item.Game.Name,
}).ToList();
Also, if I replace the Item.ProcessPersonList != null && Item.ProcessPersonList.Any() ? item.ProcessPersonList.Select(x => x.Process.Id).ToList() : new List<int>(),
by some generic int list, like a new List<int>()
, it dows not throws any error.