我在尝试获取要显示的列表的值时收到异常错误。我试图为每个用户获取数据库中的所有项目数据。我认为我的LINQ表达式是错误的,但我不确定。
型号:
public class ListModel
{
public List<StaffModel> StaffModelList { get; set; }
}
public class StaffModel
{
public FlexModel Flex { get; set; }
public List<Project> Projects { get; set; }
}
public partial class Project
{
public Project()
{
TimesheetEntries = new HashSet<TimesheetEntry>();
UserProjects = new HashSet<UserProject>();
}
[StringLength(50)]
public string ProjectId { get; set; }
[Required]
[StringLength(50)]
public string ProjectName { get; set; }
public int ProjectManager { get; set; }
public bool IsDebitable { get; set; }
[StringLength(50)]
public string Customer { get; set; }
public decimal EstimatedTotalHours { get; set; }
[Column(TypeName = "date")]
public DateTime ProjectStart { get; set; }
[Column(TypeName = "date")]
public DateTime? ProjectEnd { get; set; }
public virtual User User { get; set; }
public virtual ICollection<TimesheetEntry> TimesheetEntries { get; set; }
public virtual ICollection<UserProject> UserProjects { get; set; }
}
查看:
@model Aviato.ViewModel.ListModel
<table class="table">
@foreach (var item in Model.StaffModelList)
{
<tr>
<td>@item.Flex.User.UserId</td>
<td>@item.Flex.FlexTime</td>
<td>@item.Flex.User.SocialSecurityNumber</td>
<td>@item.Flex.User.FirstName</td>
<td>@item.Flex.User.LastName</td>
<td>@item.Flex.User.Address1</td>
<td>@item.Flex.User.ZipCode</td>
<td>@item.Flex.User.City</td>
<td>@item.Flex.User.PhoneNumber1</td>
<td>@item.Flex.User.EmploymentStartDate</td>
<td>@item.Flex.User.Password</td>
@foreach (var items in item.Projects)
{
<td>@items.ProjectId</td>
<td>@items.ProjectName</td>
<td>@items.ProjectManager</td>
<td>@items.IsDebitable</td>
<td>@items.Customer</td>
<td>@items.EstimatedTotalHours</td>
<td>@items.ProjectStart</td>
<td>@items.ProjectEnd</td>
}
</tr>
@Html.ActionLink("Edit", "Edit", new { id = item.Flex.User.UserId })
@Html.ActionLink("Delete", "Delete", new { id = item.Flex.User.UserId })
}
</table>
控制器:
private readonly AviatoModel _db = new AviatoModel(); //Database
public ActionResult Index()
{
var projects = _db.Projects.ToList();
var users = _db.Users.Where(u => u.Projects == projects).ToList(); //Where the Error occurs if I use: "ToList();"
var model = new ListModel();
var flexModel = new FlexModel();
model.StaffModelList = new List<StaffModel>();
foreach (var u in users) //Where the Error occurs if I dont use: "ToList();"
{
var staffModel = new StaffModel();
staffModel.Projects = projects;
staffModel.Flex = flexModel;
staffModel.Flex.User = u;
staffModel.Flex.FlexTime = GetFlex.Flex(u.UserId);
model.StaffModelList.Add(staffModel);
}
return View(model);
}
请帮帮我......
答案 0 :(得分:1)
这是你的问题:
var projects = _db.Projects.ToList();
var users = _db.Users.Where(u => u.Projects == projects).ToList(); //Where the Error occurs if I use: "ToList();"
而是尝试:
var users = _db.Projects.Select(p => p.User).Distinct();
答案 1 :(得分:0)
将您的操作更改为此。这样,无论用户是否有任何项目,您都可以获得所有用户。
public ActionResult Index()
{
var users = _db.Users.Include("Projects").ToList();
var model = new ListModel();
var flexModel = new FlexModel();
model.StaffModelList = new List<StaffModel>();
foreach (var u in users)
{
var staffModel = new StaffModel();
staffModel.Projects.AddRange(u.Projects); //These are user specific projects. If there are no projects assigned to user, this list will be empty.
staffModel.Flex = flexModel;
staffModel.Flex.User = u;
staffModel.Flex.FlexTime = GetFlex.Flex(u.UserId);
model.StaffModelList.Add(staffModel);
}
return View(model);
}