Reputation: 43
With Entity Framework 4 (Legacy) i would be able to query using ObjectQuery for example:
// 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.
what i want to know what is the EF6 equivalent of the code above. been stuck for 2 weeks. any help would be greatly appreciated. Thanks friends
Upvotes: 1
Views: 1622
Reputation: 1537
Though not recommended or the standard approach, you can cast the DbContext
to get the ObjectContext
and work with that:
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?
Upvotes: 2