[英]ObjectQuery<T> equivalent in Entity Framework 6
With Entity Framework 4 (Legacy) i would be able to query using ObjectQuery for example: 使用Entity Framework 4(旧版),我可以使用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;
}
so when i select shoes in combo box the grid should only show shoes pType which is a 1 with shoes description. 因此,当我在组合框中选择鞋子时,网格应仅显示鞋子pType(鞋子描述为1)。
what i want to know what is the EF6 equivalent of the code above. 我想知道什么是上面代码的EF6等效项。 been stuck for 2 weeks.
被困了两个星期。 any help would be greatly appreciated.
任何帮助将不胜感激。 Thanks friends
谢谢朋友
Though not recommended or the standard approach, you can cast the DbContext
to get the ObjectContext
and work with that: 尽管不推荐使用,也不推荐使用标准方法,但可以将
DbContext
转换为ObjectContext
并使用它:
using (var dbContext = new MyContext())
{
var objectContext = ((IObjectContextAdapter)dbContext).ObjectContext;
var itemsToBeFiltered = objectContext.CreateQuery<Product>("sql here...", params);
}
EDIT: Just linking to the next question the OP had: Entity Framework 6 + C# passing a combobox.SelectedValue as a parameter for context.CreateQuery something simple i am missing? 编辑:只是链接到OP的下一个问题: 实体框架6 + C#传递combobox.SelectedValue作为context.CreateQuery的参数我缺少一些简单的东西?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.