Lambda表达式:如何Concat 2字段?

时间:2021-03-15 19:06:10

I want to merge 2 database field and populate as an item to Dropdownlist by using Lambda Expression. Here is my lambda expression for populating dropdownlist on my Controller. I have tried lots of combination, but I could not merge the fields. Could you help me please how can I do this? Thanks.

我想合并2个数据库字段,并使用Lambda Expression将项目填充到Dropdownlist。这是我的lambda表达式,用于在Controller上填充下拉列表。我尝试了很多组合,但我无法合并这些字段。你能帮帮我吗我怎么能这样做?谢谢。

Note : Here I want to merge CityName and MeetingDate fields like Paris 01.01.2014 as one item of dropdownlist.

注意:这里我想将CityName和MeetingDate字段(例如Paris 01.01.2014)合并为下拉列表中的一项。

 private void PopulateMeetingsDropDownList(object selectedMeetings = null)
    {
        var meetingsQuery = repository.Meetings
            .Join(repository.Cities, m => m.MeetingCityId, c => c.CityID,
                    (m, c) => new
                    {
                        CityID = c.CityID,
                        CityName = c.CityName,
                        MeetingDate=m.MeetingStartDate
                    })
             .OrderBy(x => x.CityID).ToList();
        ViewData["MeetingId"] = new SelectList(meetingsQuery, "CityID", "CityName", selectedMeetings);
    }        

2 个解决方案

#1


1  

What about instead of separated CityName and MeetingDate return concatenated DisplayValue.

而不是分开的CityName和MeetingDate返回连接的DisplayValue。

Updated code:

更新的代码:

private void PopulateMeetingsDropDownList(object selectedMeetings = null)
{
    var meetingsQuery = repository.Meetings
        .Join(repository.Cities, m => m.MeetingCityId, c => c.CityID,
            (m, c) => new {
                CityID = c.CityID,
                CityName = c.CityName,
                MeetingDate=m.MeetingStartDate
            }
        )
        .OrderBy(x => x.CityID)
        .AsEnumerable()
        .Select(
            i => new {
                CityID = i.CityID,
                DisplayValue = string.Format(
                    "{0} {1:dd.MM.yyyy}", 
                    i.CityName, i.MeetingDate)
            }
        ).ToList();
    ViewData["MeetingId"] = new SelectList(meetingsQuery, "CityID", "DisplayValue", selectedMeetings);
}

.AsEnumerable() "splits" this query to two parts, first LINQ2SQL and the second LINQ2OBJECT. The 1st part will be executed in database, the 2nd in .NET (locally). Usually this is preferred way (hint: performance) - filtering, sorting, grouping, etc. on db, other things in application.

.AsEnumerable()将此查询“拆分”为两部分,第一部分是LINQ2SQL,第二部分是LINQ2OBJECT。第一部分将在数据库中执行,第二部分在.NET(本地)中执行。通常这是首选方式(提示:性能) - db上的过滤,排序,分组等,应用程序中的其他内容。

More details: https://*.com/a/17996264/1027198, https://*.com/a/17968688/1027198.

更多详细信息:https://*.com/a/17996264/1027198,https://*.com/a/17968688/1027198。

#2


0  

You rock! Thank you very much for your great help. Actually I have encountered "LINQ to Entities does not recognize the method 'System.String Format(System.String, System.Object, System.Object)' method, and this method cannot be translated into a store expression." error message after first applying your method, but by adding ".ToList()" to the third line I have succeeded. So, for informing those who might need this great functionality here is the last state of the method in my Controller:

你摇滚!非常感谢你的大力帮助。其实我遇到过“LINQ to Entities无法识别方法'System.String Format(System.String,System.Object,System.Object)'的方法,而且这个方法无法翻译成商店表达式。”首次应用您的方法后出现错误消息,但通过将“.ToList()”添加到第三行我已成功。因此,为了通知那些可能需要这个强大功能的人,这是我的Controller中方法的最后一个状态:

private void PopulateMeetingsDropDownList(object selectedMeetings = null)
{
    var meetingsQuery = repository.Meetings.ToList()
        .Join(repository.Cities, m => m.MeetingCityId, c => c.CityID,
                (m, c) => new
                {
                    CityID = c.CityID,
                    DisplayValue = string.Format("{0} ({1:dd MMMM yyyy})", c.CityName, m.MeetingStartDate) 
                })
       .OrderBy(x => x.CityID).ToList();
    ViewData["MeetingId"] = new SelectList(meetingsQuery, "CityID", "DisplayValue", selectedMeetings); 
}


I think there is no problem with using ".ToList()" 2 times on the 3rd and 10th lines. Could you clarify please? Thank you very much again for your help.
BR.

我认为在第3行和第10行使用“.ToList()”2次没有问题。你能澄清一下吗?再次感谢您的帮助。 BR。

#1


1  

What about instead of separated CityName and MeetingDate return concatenated DisplayValue.

而不是分开的CityName和MeetingDate返回连接的DisplayValue。

Updated code:

更新的代码:

private void PopulateMeetingsDropDownList(object selectedMeetings = null)
{
    var meetingsQuery = repository.Meetings
        .Join(repository.Cities, m => m.MeetingCityId, c => c.CityID,
            (m, c) => new {
                CityID = c.CityID,
                CityName = c.CityName,
                MeetingDate=m.MeetingStartDate
            }
        )
        .OrderBy(x => x.CityID)
        .AsEnumerable()
        .Select(
            i => new {
                CityID = i.CityID,
                DisplayValue = string.Format(
                    "{0} {1:dd.MM.yyyy}", 
                    i.CityName, i.MeetingDate)
            }
        ).ToList();
    ViewData["MeetingId"] = new SelectList(meetingsQuery, "CityID", "DisplayValue", selectedMeetings);
}

.AsEnumerable() "splits" this query to two parts, first LINQ2SQL and the second LINQ2OBJECT. The 1st part will be executed in database, the 2nd in .NET (locally). Usually this is preferred way (hint: performance) - filtering, sorting, grouping, etc. on db, other things in application.

.AsEnumerable()将此查询“拆分”为两部分,第一部分是LINQ2SQL,第二部分是LINQ2OBJECT。第一部分将在数据库中执行,第二部分在.NET(本地)中执行。通常这是首选方式(提示:性能) - db上的过滤,排序,分组等,应用程序中的其他内容。

More details: https://*.com/a/17996264/1027198, https://*.com/a/17968688/1027198.

更多详细信息:https://*.com/a/17996264/1027198,https://*.com/a/17968688/1027198。

#2


0  

You rock! Thank you very much for your great help. Actually I have encountered "LINQ to Entities does not recognize the method 'System.String Format(System.String, System.Object, System.Object)' method, and this method cannot be translated into a store expression." error message after first applying your method, but by adding ".ToList()" to the third line I have succeeded. So, for informing those who might need this great functionality here is the last state of the method in my Controller:

你摇滚!非常感谢你的大力帮助。其实我遇到过“LINQ to Entities无法识别方法'System.String Format(System.String,System.Object,System.Object)'的方法,而且这个方法无法翻译成商店表达式。”首次应用您的方法后出现错误消息,但通过将“.ToList()”添加到第三行我已成功。因此,为了通知那些可能需要这个强大功能的人,这是我的Controller中方法的最后一个状态:

private void PopulateMeetingsDropDownList(object selectedMeetings = null)
{
    var meetingsQuery = repository.Meetings.ToList()
        .Join(repository.Cities, m => m.MeetingCityId, c => c.CityID,
                (m, c) => new
                {
                    CityID = c.CityID,
                    DisplayValue = string.Format("{0} ({1:dd MMMM yyyy})", c.CityName, m.MeetingStartDate) 
                })
       .OrderBy(x => x.CityID).ToList();
    ViewData["MeetingId"] = new SelectList(meetingsQuery, "CityID", "DisplayValue", selectedMeetings); 
}


I think there is no problem with using ".ToList()" 2 times on the 3rd and 10th lines. Could you clarify please? Thank you very much again for your help.
BR.

我认为在第3行和第10行使用“.ToList()”2次没有问题。你能澄清一下吗?再次感谢您的帮助。 BR。