當試圖獲取要顯示的列表的值時,會出現不尋常的錯誤。我試圖讓每個用戶的數據庫中的所有項目數據。我認爲我的LINQ表達是錯誤的,但我不確定。無法創建類型爲''的常量值。只有原始類型或枚舉類型在此上下文中受支持。 ASP.NET MVC LINQ EF
型號:
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);
}
請幫我...
'_db.Users.Where(u => u.Projects == projects)'這是你的問題 - 你在這裏做什麼? – 2014-10-07 14:12:42