我有一个显示报告的详细信息页面,我现在需要一个显示多个报告的索引页面。我已经设置了一些成功(我可以选择用户页面上的复选框,并在'打印页面'上显示一些信息,但我无法弄清楚如何显示相关信息。
我的详细信息包含这样的用户ID -
public ViewResult Details(string id)
{
User user = db.Users
.Include(i => i.UserDetails)
.Include(i => i.TRS)
.Include(i => i.Pension)
.Include(i => i.LifeAssurance)
.Include(i => i.IncomeProtection)
.Where(i => i.UserName == id)
.Single();
string pensionProviderName = providerRepository.GetProviderByID(user.Pension.PensionProviderID).ProviderName;
TRSViewModel viewModel = new TRSViewModel
{
UserName = user.UserName,
FirstName = user.UserDetails.FirstName,
LastName = user.UserDetails.LastName,
Salary = user.UserDetails.Salary,
PensionSchemeName = pensionSchemeName
但在我的索引中我有
public ViewResult Index(FormCollection form)
{
string[] UserIDs = form["TRSIDs"].Split(',');
var trs = db.Users
.Include(t => t.UserDetails)
.Include(t => t.TRS)
.Include(t => t.Pension)
.Where(g => UserIDs.Contains(g.UserName))
.Select(user => new TRSViewModel
{
UserName = user.UserName,
FirstName = user.UserDetails.FirstName,
LastName = user.UserDetails.LastName,
Salary = user.UserDetails.Salary
但我无法弄清楚如何在视图模型中包含'PensionSchemeName'详细信息。我尝试使用 -
直接映射PensionSchemeName = db.SchemeNames.Find(user.Pension.PensionSchemeNameID).Name,
但是我得到了一个错误。我有什么想法可以包含这个吗?
答案 0 :(得分:1)
您可以尝试手动加入用户和SchemeNames:
public ViewResult Index(FormCollection form)
{
string[] UserIDs = form["TRSIDs"].Split(',');
var trs = db.Users
.Join(db.SchemeNames,
user => user.Pension.PensionSchemeNameID,
schemeName => schemeName.SchemeNameID,
(user, schemeName) => new { User = user, SchemeName = schemeName })
.Where(a => UserIDs.Contains(a.User.UserName))
.Select(a => new TRSViewModel
{
UserName = a.User.UserName,
FirstName = a.User.UserDetails.FirstName,
LastName = a.User.UserDetails.LastName,
Salary = a.User.UserDetails.Salary,
PensionSchemeName = a.SchemeName.Name
}
//...
}
我认为Pension
没有PensionSchemeName
导航属性,否则会更直接:
var trs = db.Users
.Where(u => UserIDs.Contains(u.UserName))
.Select(u => new TRSViewModel
{
UserName = u.UserName,
FirstName = u.UserDetails.FirstName,
LastName = u.UserDetails.LastName,
Salary = u.UserDetails.Salary,
PensionSchemeName = u.Pension.PensionSchemeName.Name
}
(我已移除了Include
。当您使用投影(Select
)时,它们会被忽略。)