I have this sentence linq, with select inside a select using a two viewmodels, the problem is that in my loginviewmodel not all have a status equal to received, there may be nulls, but when putting them in my view some columns in my grid appear empty, I would like to see N/A appear by default if logviewmodel is = null but I don't know how to do it, I return this as JSON since I'm using datatable on the server side, I don't know if manipulating the json can also be done
I Share my linq sentence
var result = db.document.Select(d => new DocumentViewModel
{
DocumentId = d.DocumentId,
Name = w.name
ReceivedLogs = d.Logs
.Where(l => l.Status == Status.Received)
.Select(l => new LogViewModel
{
CurrentApprover = l.User,
NameApprover = l.User.FullName
}).FirstOrDefault()
}).ToList();
thanks
You can check if FirstOrDefault() returns null and then use a default LogViewModel:
var result = db.document.Select(d => new DocumentViewModel
{
DocumentId = d.DocumentId,
Name = w.name
ReceivedLogs = d.Logs
.Where(l => l.Status == Status.Received)
.Select(l => new LogViewModel
{
CurrentApprover = l.User,
NameApprover = l.User.FullName
}).FirstOrDefault() ?? new LogViewModel {
CurrentApprover = "N/A",
NameApprover = "N/A"
}
}).ToList();