ObjectContext context = ((IObjectContextAdapter)db).ObjectContext;
string queryString = @\"SELECT VALUE c FROM Product AS c WHERE c.ProductType = \" + comboBox1.Selecte
You have to add a parameter to your query, that is:
var context = ((IObjectContextAdapter)db).ObjectContext;
var queryString = @"SELECT VALUE c FROM Product AS c WHERE c.ProductType = @productType";
var productQuery = context.CreateQuery<Product>(queryString, new ObjectParameter("productType", comboBox1.SelectedValue);
I will tell you the reason in a bit. Let's go through this code first so I can explain what the issue could be.
Here is a Person
class:
public class Person
{
public int Age { get; set; }
public string Name { get; set; }
}
Here is a list we will bind the combobox to:
var persons = new List<Person>() { new Person { Age = 35, Name = "George" } };
Here are different ways to set the binding:
comboBox1.DataSource = persons;
comboBox1.DisplayMember = "Name";
comboBox1.ValueMember = "Name";
combobox1.SelectedValue
will return "George".
Here is another way to set the binding:
comboBox1.DataSource = persons;
comboBox1.DisplayMember = "Name";
combobox1.SelectedValue
will return a Person
object.
Your issue is most likely because SelectedValue
is not returning what you expect it to return.