实体框架6 + C#传递combobox.SelectedValue作为context.CreateQuery的参数我缺少一些简单的东西吗? [英] Entity Framework 6 + C# passing a combobox.SelectedValue as a parameter for context.CreateQuery something simple i am missing?

查看:40
本文介绍了实体框架6 + C#传递combobox.SelectedValue作为context.CreateQuery的参数我缺少一些简单的东西吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

ObjectContext context = ((IObjectContextAdapter)db).ObjectContext; 
string queryString = @"SELECT VALUE c FROM Product AS c WHERE c.ProductType = " + comboBox1.SelectedValue; 
ObjectQuery<Product> productQuery = context.CreateQuery<Product>( queryString, comboBox1.SelectedValue ); 

//我在传递combobox.selectedvalue作为参数时遇到麻烦–

// I am having trouble passing combobox.selectedvalue as a parameter –

推荐答案

我稍后将告诉您原因.让我们先看一下这段代码,以便我可以解释问题的根源.

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.

这是一个 Person 类:

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 将返回乔治".

这是设置绑定的另一种方法:

Here is another way to set the binding:

comboBox1.DataSource = persons;

comboBox1.DisplayMember = "Name";

combobox1.SelectedValue 将返回一个 Person 对象.

您的问题很可能是因为 SelectedValue 未返回您期望的返回值.

Your issue is most likely because SelectedValue is not returning what you expect it to return.

这篇关于实体框架6 + C#传递combobox.SelectedValue作为context.CreateQuery的参数我缺少一些简单的东西吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆