var term = from s in DB.Terms
select new { s.Term_ID, s.TermName };
comboboxTerm.DataSource = term;
combobox.DisplayMember = "Term_Name";
combobox.ValueMember = "Term_ID";
我想将上述查询转换为列表<>这样我就可以将列表设置为数据源到comboboxTerm。这样我就可以在代码中访问comboboxTerm selectedValue。
答案 0 :(得分:0)
comboboxTerm.DataSource = term.ToList();
OR
term = term.ToList();
答案 1 :(得分:0)
将其更改为:
var term = (from s in DB.Terms
select new { s.Term_ID, s.TermName }).ToList();
这实际上也会解析您的查询,因为默认情况下您返回的是IQueryable
但,你最好使用字典,如下所示:
var term = (from s in DB.Terms
select new { s.Term_ID, s.TermName })
.ToDictionary(s => s.Term_ID, s => s.TermName);
然后将以下内容添加到组合框中:
SelectedValuePath="Key" DisplayMemberPath="Value"