0

我有一个显示报告的详细信息页面,我现在需要一个显示多个报告的索引页面。我已经成功地进行了设置(我可以选择用户页面上的复选框并在“打印页面”上显示一些信息,但我不知道如何显示相关信息。

我的详细信息包含这样的用户 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,

但我得到了一个错误。有什么想法可以包括这个吗?

4

1 回答 1

1

您可以尝试手动加入用户和方案名称:

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
        }

(我已经删除了Includes。当您使用投影 ( Select) 时,它们无论如何都会被忽略。)

于 2013-01-14T18:51:37.590 回答