Entity Framework 6中的ObjectQuery <t>等价物

时间:2016-12-13 02:27:19

标签: c# sql-server entity-framework visual-studio-2013 linq-to-entities

使用Entity Framework 4(Legacy),我将能够 例如,使用ObjectQuery进行查询:

// where Product is a table in a sample 
// database created with sql management studio
// Product table has a foreign key pType to another table p_type and there is a pType key there
// pType is of type int and another column that is called description which describes the pType
// 1 would be shoes 2 would be hats etc.
// filteredCombobox is data boung pType(displaymember) and Description(valuemember) 
// dbcontext is the database Entity

//this event below is the SelectionChangeCommit
        private void listToBeFiltered(object sender, EventArgs e)
{
    ObjectQuery<Product> itemsToBeFiltered = new ObjectQuery<Product>(
    "Select Value c FROM Product AS c WHERE c.pType = " + filterCombobox.SelectedValue, dbcontext);

    dataGridView1.DataSource = itemsToBeFiltered;
}

所以当我在组合框中选择鞋子时,网格应该只显示鞋子pType,这是鞋子描述的1。

我想知道什么是上面代码的EF6等价物。被困2周。 任何帮助将不胜感激。谢谢朋友们

1 个答案:

答案 0 :(得分:2)

虽然不推荐或采用标准方法,但您可以投放DbContext以获取ObjectContext并使用该标准:

using (var dbContext = new MyContext())
{
    var objectContext = ((IObjectContextAdapter)dbContext).ObjectContext;
    var itemsToBeFiltered = objectContext.CreateQuery<Product>("sql here...", params);
}

编辑:只需链接到OP的下一个问题:Entity Framework 6 + C# passing a combobox.SelectedValue as a parameter for context.CreateQuery something simple i am missing?