1

我可以使用PadLeft()Razor View 中的方法,如下所示,没有任何问题:

@Model.Project.PKey - @Model.IssueNum.ToString().PadLeft(5, '0')

但是,我无法使用相同的视图模型在控制器中创建相同的语句:

var result = dataContext.Select(m => new
            IssueViewModel
            {
                ID = m.ID,
                ProjectID = m.ProjectID
                Key = m.Project.PKey + "-" + m.IssueNum.ToString().PadLeft(5, '0')
            }
        ).ToDataSourceResult(request);

您对如何在其中使用PadLeft()方法有任何想法lambda expression吗?

4

1 回答 1

2

在使用之前,您需要将数据从数据源放入内存PadLeft

var result = dataContext.Select(
    m => new { // Make a projection of all columns we need into an anonymous type
        ID = m.ID,
        ProjectID = m.ProjectID,
        PKey = m.Project.PKey,
        IssueNum = m.IssueNum
    }) // Now bring the result from the data source into memory
    .AsEnumerable()
    // We are ready to make IssueModel from the parts prepared above:
    .Select(m => new IssueModel {
         ID = m.ID,
         ProjectID = m.ProjectID,
         Key = m.PKey + "-" + m.IssueNum.ToString().PadLeft(5, '0')
     })
     .ToDataSourceResult(request);

现在IssueModel由支持PadLeft.

于 2015-08-25T21:18:18.853 回答