Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Concatenating two fields in LINQ select

I have a dropdownlist, ddCourse, that I'm populating with the following LINQ query:

var db = new DataClasses1DataContext();
ddCourse.DisplayMember = "COURSE_TITLE";
ddCourse.ValueMember = "COURSE_ID";
ddCourse.DataSource = db.COURSE_MASTERs.OrderBy(c => c.COURSE_TITLE)
                                       .Select(c => new { c.COURSE_ID, c.COURSE_TITLE })
                                       .ToList();

There's another field, though, that I'd like to concatenate to the COURSE_TITLE field in my selection. So, I'd like my selection to look like:

.Select( c => new {c.COURSE_ID, c.CIN + " " + c.COURSE_TITLE})

The only problem is that this, apparently, isn't how it's done. I'm basically wanting to join c.CIN with c.COURSE_TITLE (and have a space in the middle). Can someone offer me some pointers on how to accomplish this?

The reason I want to do this is that, right now, the only thing appearing in the dropdownlist is the course title. I'd like to have the course ID number (CIN) concatenated to it when it displays.

EDIT: For clarification, I'm using Linq-to-SQL.

like image 427
Kevin Avatar asked Sep 20 '13 20:09

Kevin


2 Answers

use this

.Select( c => new {c.COURSE_ID, COURSE_TITLE =string.Format("{0} {1}" ,c.CIN ,c.COURSE_TITLE)})
like image 86
Thilina H Avatar answered Sep 19 '22 00:09

Thilina H


You need to name your anonymous members:

.Select( c => new {COURSE_ID = c.COURSE_ID, COURSE_TITLE = c.CIN + " " + c.COURSE_TITLE})
like image 45
SouthShoreAK Avatar answered Sep 17 '22 00:09

SouthShoreAK