I have a simple Index View, and a controller (using Entity Framework Core). Model "WorkTask" shows a number of tasks (the main model in the view). Model "WorkTaskLog" contains log entries per user for the specific task.
The view is based on a ViewModel with mainly a WorkTask model. I wish to lookup field "StatusName" in model called WorkTaskLog so I can show that in the view (as a list).
- I wish to show all records in the recordset from WorkTask, nomatter if no records exists in WorkTaskLog.
- I wish to add a "Where Username = currUserName" filter to the WorkTaskLog
- All should end up as a list so I can loop through content in my view
I have tried joining the 2 tables below without luck, but I'm out of ideas. My best attempt would be this (from my Index task in the controller, just before redirect to the view):
WorkTaskListViewModel model = new WorkTaskListViewModel()
{
WorkTask = from worktask in _db.WorkTask
.Where(l => l.WeekNumber == currWeekNo)
.Where(m => m.YearNumber == currYearNo)
.Where(n => n.IsActive == true)
.Where(o => o.TaskDate == DateTime.Today)
.Include(p => p.WorkType)
.Include(q => q.Item)
.Include(r => r.Customer)
join worktasklog in _db.WorkTaskLog on worktask.Id equals
worktasklog.WorkTaskId into wtasks
select new { worktask, wtasks.ToList()},
itemList = await _db.Item.ToListAsync(),
customerList = await _db.Customer.ToListAsync(),
YearNumber = (int)currYearNo,
WeekNumber = (int)currWeekNo,
Msg = msg,
UserName = currUserName
};
wtasks.ToList()
is obviously wrong since the compiler rejects it.
I don't have a feeling if I'm even close, but that I hope any of you guys can help solving my puzzle?
Thanks a lot in advance for your help.
EDIT: I have tried with a little different approach, and now it almost works:
var model = (from a in _db.WorkTask
join b in _db.WorkTaskLog on a.Id equals b.WorkTaskId into joinedT
from b in joinedT.DefaultIfEmpty()
where b.UserName == currUserName || b.UserName == null
select new DayTaskListViewModel
{
Id = a.Id,
DateFormatted = a.DateFormatted,
HasRegistrations = a.HasRegistrations,
HasStartedRegistrations = a.HasStartedRegistrations,
ItemName = a.ItemName,
WorkTypeName = a.WorkTypeName,
Description = a.Description,
IsActive = b.IsActive ? b.IsActive : false,
UserName = b.UserName ?? String.Empty,
StateStatus = b.StateStatus ?? "Klar",
WeekNumber = (int)currWeekNo,
YearNumber = (int)currYearNo,
Msg = "",
SignInUserName = currUserName
}).ToList();
Only 2 issues left:
1. The WHERE line: "where b.UserName == currUserName || b.UserName == null" As this is a leftjoin I only want ALL records from "WorkTask" nomatter if there are related records in "WorkTaskLog" or not. With my above filter it only filters the username match, but if there are no records in "WorkTaskLog" it doesn't show ALL records from "WorkTask". If the WHERE filter could only select the last entry in the "WorkTaskLog" matching username criteria, that would be optimal.
2. I only wish ALL records from "WorkTask" shown ONCE. Now, if there are several records in "WorkTaskLog" which matches, then the "WorkTask" is shown as many times as duplicates. Would there be a way to DISTINCT the "WorkTask" model?
I have experimented with this all day, and I can no longer think. Kindly ask for a little help :-)