I'm not sure the title explains the problem well, so here is the code. I want to return an anonymous object to bind my grid to. In that grid, I want to change usernames to fullnames, but only if I HAVE the fullname to match the username. In the query below, I get CreatedFullname IF CreatedUsername exists in userModels. If it does not exists then I don't get the record at all. What I wnat is to get the record, show UserFullname if it is available in userModels, if it is not I want to return x.CreatedUsername.
var inf = (from ev in db.Events
where (ev.StartDate >= beginDate && ev.StartDate <= endDate)
orderby ev.StartDate descending
select new
{
EventID = ev.EventID,
EventTitle = ev.Title,
EventDate = ev.StartDate,
StudentCount = ev.EventStudents.Count(),
CreatedUsername = ev.CreatedUsername
}).AsEnumerable().Select(x => new {EventID = x.EventID,
EventTitle = x.EventTitle,
EventDate = x.EventDate,
StudentCount = x.StudentCount,
CreatedUsername = x.CreatedUsername,
CreatedFullname =
userModels.Where(u => u.Username == x.CreatedUsername).FirstOrDefault().UserFullName
});
userModels is a custom object that has Username, UserFullname, Department, etc. It was collected from ActiveDirectory on pageload, based on beginDate and endDate.