In C # 3.5, using a ComboBox to display LinQ query results. How to set the selecteditem property in a combo box when a LinQ query returns an anonymous type?
I set the dropdown list data source on these lines:
comboBox1.DataSource = from p in db.products select p; comboBox1.DisplayMember = "Name"; comboBox1.ValueMember = "ProductId";
If I do this, I can select the selected item by doing something like:
comboBox1.SelectedItem = (from p in db.products where p.ProductId = 5 select p).First();
The problem is that I want to populate combobox with an anonymous result like:
comboBox1.DataSource = from p in db.products select new { p.ProductId, p.Name };
The anonymous type that I actually use is more complex than that, but this is enough to explain.
source share