6

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.

4

3 回答 3

15

用这个

.Select( c => new {c.COURSE_ID, COURSE_TITLE =string.Format("{0} {1}" ,c.CIN ,c.COURSE_TITLE)})
于 2013-09-20T20:56:13.210 回答
6

您需要命名您的匿名成员

.Select( c => new {COURSE_ID = c.COURSE_ID, COURSE_TITLE = c.CIN + " " + c.COURSE_TITLE})
于 2013-09-20T20:51:21.147 回答
3

像这样写你的Select

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

匿名类型需要指定其列名,以防无法推断。

因为c.COURSE_IDC# 足够聪明,可以生成一个COURSE_ID在匿名类型中调用的成员。对于表达式c.CIN + " " + c.COURSE_TITLE它不能。

于 2013-09-20T20:50:17.583 回答