0

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.

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

 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);
    }        
tereško
  • 58,060
  • 25
  • 98
  • 150
Jack
  • 1
  • 21
  • 118
  • 236

2 Answers2

1

What about instead of separated CityName and MeetingDate return concatenated 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.

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

Community
  • 1
  • 1
Michał Powaga
  • 22,561
  • 8
  • 51
  • 62
  • Thank you very much Michal for your good answer. I proposed your reply as an answer. But unfortunately I cannot Vote Up your reply due to not having enough reputation :( – Jack Oct 19 '13 at 22:07
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:

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.

Jack
  • 1
  • 21
  • 118
  • 236