如何在SelectList中将LINQ写入实体Lambda和Concatenate属性

时间:2012-11-14 14:23:21

标签: asp.net-mvc-3 lambda linq-to-entities html-select

我编写了以下方法来填充DropDownList并且它可以工作。但是,我想使用Lambda表达式作为SelectList初始化程序的第一个参数而不是那里的理解查询来编写正确的LINQ查询。我的目标是将Eventm Object的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);
            }

1 个答案:

答案 0 :(得分:1)

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

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);
    }