views:

1949

answers:

2

In C# 3.5 using a ComboBox to display the results of a LinQ Query. How do I set the selecteditem property of the combobox when the LinQ query is returning an anonymous type?

I set the datasource of the combobox along these lines:

comboBox1.DataSource = from p in db.products
                   select p;
comboBox1.DisplayMember = "Name";
comboBox1.ValueMember = "ProductId";

If I do that I can choose 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 fill a combobox with an anonymous type result like:

comboBox1.DataSource = from p in db.products
                   select new
                   {
                     p.ProductId,
                     p.Name
                   };

The anonymous type I'm actually using is more complicated then that but it suffices for explanation.

Thanks.

A: 

This should do the trick

var list = (from p in db.products
              select new {
                             p.ProductId,
                             p.Name
                           }).ToList();
comboBox1.DataSource = list;
comboBox1.SelectedItem = list[0];
JaredPar
That would only add the first result from the LinQ query to the dropdown list. I want to add all the results to the list, but select a specific item afterword.
Ryan Roper
I see what you're saying, give me a sec and i'll update the answer
JaredPar
That works if you know which record in the list is the one you want by record number. It doesn't work if you set the selecteditem in a different function from the one that loads the list. Thanks again.
Ryan Roper
A: 

How about converting it to a list, then choosing the correct one from it. Since SelectedItem doesn't seem to work, you may want to try setting SelectedValue.

var productList = (from p in db.products
                   select new {
                      ProductId = p.ProductID,
                      Name = p.Name
                   }).ToList();

comboBox1.DataSource = productList;
comboBox1.DisplayMember = "Name";
comboBox1.ValueMember = "ProductId";
comboBox1.SelectedValue = 5;
tvanfosson
I just tried it, it doesn't seem to change the selected item. It also only works if done right when it's loaded. The selected item may get set in another function. Thanks.
Ryan Roper
Did you try setting SelectedValue to the correct ProductId instead. I'll update the code.
tvanfosson
Also, I had an error in the selection logic. I've fixed that -- should have been a Where method instead of a Select method. Changing that may make the previous example work.
tvanfosson
Actually this caused me to stumble on the answer. Apparently once you set the valuemember property to the name of the field you want in the anon type just setting the SelectedValue = 5 changes the selected item.
Ryan Roper
Sure. I don't know why I was thinking you had to set the name.
tvanfosson