1

我编写了以下方法来填充 DropDownList 并且它有效。但是,我想使用 Lambda 表达式作为 SelectList 初始化程序的第一个参数而不是那里的理解查询来编写正确的 LINQ 查询。我的目标是连接 Eventm 对象的 EventmId 和 EventmTitle 属性,以在 DropDownList 中显示为选择项。

谢谢!

     private void PopulateEventmsDropDownList(object selectedEventm = null)
            {
                var eventmsQuery = unitOfWork.EventmRepository.Get(
                                       orderBy: q => q.OrderBy(d => d.EventmId));

                ViewBag.EventmID = new SelectList(
                (from s in eventmsQuery select new { EventmId = s.EventmId.ToString(),
                FullID = s.EventmId + " " + s.EvtTitle }),
                "EventmId", "FullID", selectedEventm);
            }
4

1 回答 1

1

在使用 LINQPad 进行一些试验和错误之后,我在 SelectList 中提出了这个 Lambda 表达式(以替换理解查询),如下所示:

private void PopulateEventmsDropDownList(object selectedEventm = null)
    {
        var eventmsQuery = unitOfWork.EventmRepository.Get(
             orderBy: q => q.OrderBy(d => d.EventmId));

        ViewBag.EventmID = new SelectList(nodesQuery.Select(
             x => new {x.NodeId, FullID =  string.Join(
             "  ", x.NodeId.ToString(), x.Title) }), "EventmId", "FullID", selectedEventm);
    }
于 2012-11-16T23:37:48.880 回答