【发布时间】:2021-12-12 00:11:30
【问题描述】:
我有以下 ViewModel:
public class DayTaskListViewModel
{
public int Id { get; set; }
public string DateFormatted { get; set; }
public bool HasRegistrations { get; set; }
public bool HasStartedRegistrations { get; set; }
public string ItemName { get; set; }
public string WorkTypeName { get; set; }
public string Description { get; set; }
public string LocationName { get; set; }
public bool IsActive { get; set; }
public string UserName { get; set; }
public string StateStatus { get; set; }
public DateTime TodaysDate { get; set; }
public int WeekNumber { get; set; }
public int YearNumber { get; set; }
public string Msg { get; set; }
public string SignInUserName { get; set; }
public string UnitCode { get; set; }
public IEnumerable<Machinery> machineryList { get; set; }
public IEnumerable<Cleaning> cleaningList { get; set; }
}
在我的控制器中,我有这个要发送到视图的模型定义:
var model = (from a in _db.WorkTask.Where(y => y.TaskDate.Date == querytodaysDate.Date && y.IsActive == true && (y.IsPrivateUserName == null || y.IsPrivateUserName == currUserName))
join b in _db.WorkTaskLog.Where(x => (x.UserName == currUserName || x.UserName == null) && x.IsActive == true && x.StateStatusId < 4) on a.Id equals b.WorkTaskId into joinedT
from c in joinedT.DefaultIfEmpty()
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 = c.IsActive ? c.IsActive : false,
UserName = c.UserName ?? String.Empty,
StateStatus = c.StateStatus ?? "Klar",
WeekNumber = (int)currWeekNo,
YearNumber = (int)currYearNo,
Msg = "",
TodaysDate = (DateTime)todaysDate,
SignInUserName = currUserName,
LocationName = a.LocationName,
UnitCode = a.UnitCode,
//machineryList = _db.Machinery.ToList(),
//cleaningList = _db.Cleaning.ToList(),
}).ToList();
我的问题是定义中的以下几行: //machineryList = _db.Machinery.ToList(), //cleaningList = _db.Cleaning.ToList(),
一切都按预期工作,但是一旦我启用这 2 行,它就会因空错误而中断。 VS 可以编译,但是会破坏运行时。
我想我看到了问题,但我不知道如何解决它。 我希望 ViewModel 中的所有字段除了提到的 2 行是一个列表,然后 2 行是独立于大多数的单独列表。 我已经尝试了移动这些线条的所有组合,但随后 VS 抱怨。
另一个控制器的示例如下:
DriveListViewModel model = new DriveListViewModel()
{
Drive = await _db.Drive
.Where(m => m.StatusId == 5 || m.StatusId == 1010 || m.StatusId == 1012)
.Where(m => m.LoadingComplete == null)
.Where(m => !m.UnitCode.Contains(excludeString))
.Include(s => s.DriveStatus)
.Include(d => d.Location)
.Include(f => f.Item)
.GroupBy(m => m.RegistrationNumber)
.Select(m => m.FirstOrDefault())
.OrderBy(m => m.DriverToLoad)
.ToListAsync(),
machineryList = await _db.Machinery.ToListAsync(),
cleaningList = await _db.Cleaning.ToListAsync(),
};
这很好用,但前一个模型定义更复杂,所以基本上,我需要类似于后一个示例的东西,将 2 个列表与 ViewModel 中的其他属性分开。 也许这很简单 - 但是我正在努力解决它......
有人看到这个问题的解决方案吗?
【问题讨论】:
-
那么...为什么不在查询之后设置这两个属性呢?执行
Select,然后执行var machineryList = await _db.Machinery.ToListAsync(); var cleaningList = await _db.Cleaning.ToListAsync(); model.ForEach(m => { m.machineryList = machineryList; m.cleaningList = cleaningList; }); -
你好异端。我真的没有接受你的建议!非常感谢,它现在有效,我可以继续。非常感谢。你和 Serge ROCKS!!!