0

我很难从 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);

    }
}

}

4

1 回答 1

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();
于 2016-12-07T00:05:39.623 回答