我很难从db_Entities模型中过滤数据。我有一个绑定到comboBox1的表,我试图使用comboBox1控件中的commitChanged事件过滤数据。我收到2个错误。 1是:'System.Data.Entity.Core.Objects.ObjectQuery.ObjectQuery(string,System.Data.Entity.Core.Objects.ObjectContext)'的最佳重载方法匹配有一些无效的参数。
第二个是:无法从'SmallStore.db_Entities'转换为'System.Data.Entity.Core.Objects.ObjectContext'。我离开目录以节省空间。这是代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.Entity.Core.Objects;
using System.Data.Entity.Core.Query;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace SmallStore
{
public partial class ViewProducts : Form
{
private db_Entities db = new db_Entities();
public ViewProducts()
{
InitializeComponent();
dataGridView1.DataSource = db.Products.ToList();
dataGridView1.Columns["ProductType"].Visible = false;
dataGridView1.Columns["TransactionItems"].Visible = false;
dataGridView1.Columns["Product_Type"].Visible = false;
comboBox1.DataSource = db.Product_Type.ToList();
comboBox1.ValueMember = "Description";
comboBox1.DisplayMember = "Product_Type";
}
private void changeCommit(object sender, EventArgs e)
{
/* filter products */
ObjectQuery<Product> filteredProducts = new ObjectQuery<Product>("SELECT VALUE item FROM Products AS item WHERE item.ProductType = " + comboBox1.SelectedValue, db);
}
}
}
答案 0 :(得分:0)
试试这个:
var querystring = "SELECT VALUE item FROM Products AS item WHERE item.ProductType = " + comboBox1.SelectedValue;
Debugger.Break(); //Ensure querystring is what you want here
var products = db.Database.SqlQuery<Product>(querystring).ToList();