Combining two fields in LINQ select

I have a dropdownlist, ddCourse, which I populate 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 is another field in this field that I would like to associate with the COURSE_TITLE field in my selection. So, I would like my choice to look like this:

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

The only problem is that this is apparently not the way it was done. I basically want to join c.CIN with c.COURSE_TITLE (and have a place in the middle). Can someone suggest me some pointers on how to do this?

The reason I want to do this is because now the only thing that appears in the drop-down list is the name of the course. I would like the course identifier (CIN) to be combined with it when it is displayed.

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

+4
source share
3 answers

use this

 .Select( c => new {c.COURSE_ID, COURSE_TITLE =string.Format("{0} {1}" ,c.CIN ,c.COURSE_TITLE)}) 
+12
source

You need to call anonymous members :

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

Write your Select as follows:

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

Anonymous types must have specified column names if they cannot be inferred.

For c.COURSE_ID C # is smart enough to generate a member called COURSE_ID in an anonymous type. For the expression c.CIN + " " + c.COURSE_TITLE it cannot.

+3
source

All Articles