我创建了一个linq,结果是给了我一个itemtypes列表,我试图将列表绑定到组合框,但是我为第一个组合框设置了DisplayMember和ValueMember之后很奇怪,然后load事件似乎返回,并且从不执行其余的代码
private void AddOpenOrderForm_Load(object sender, EventArgs e)
{
using (context = new MRP_DBEntities())
{
var query1 = (from i in context.LookUPs select i.itemType).Distinct();
itemType1CB.DataSource = query1.ToList();
itemType1CB.DisplayMember = "itemType";
itemType1CB.ValueMember = "itemType";
MessageBox.Show("The program will not reach the follow codes");
itemType2CB.DataSource = query1.ToList();
itemType2CB.DisplayMember = "itemType";
itemType2CB.ValueMember = "itemType";
}
}
答案 0 :(得分:2)
我的猜测是,你得到了一个你只是没有看到的例外。您已经在此处选择 itemType
成员:
var query1 = (from i in context.LookUPs select i.itemType).Distinct();
...但是您在这里尝试找到每个值的itemType
成员:
itemType1CB.DisplayMember = "itemType";
itemType1CB.ValueMember = "itemType";
我怀疑您应该将DisplayMember
和ValueMember
保留为""
,以便您自己显示对象。
我还建议您调用ToList
一次,而不是使用查询表达式,它不会真正减少混乱:
var list = context.LookUPs.Select(i => i.itemType).Distinct().ToList();
itemType1CB.DataSource = list;
itemType2CB.DataSource = list;
// Unless you've set the display/value members elsewhere, they'll default to ""