1

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

artm
  • 8,554
  • 3
  • 26
  • 43
Soundview
  • 43
  • 7
  • See this: http://www.entityframeworktutorial.net/Querying-with-EDM.aspx the section on Entity SQL – DVT Dec 13 '16 at 02:37
  • Possible duplicate of [Entity Framework - Where is my Object Context?](http://stackoverflow.com/questions/14245153/entity-framework-where-is-my-object-context) – Andrés Robinet Dec 13 '16 at 02:44

1 Answers1

2

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?

Community
  • 1
  • 1
Andrés Robinet
  • 1,527
  • 12
  • 18
  • I tried using your solution nothing happened when i triggered change commit event with my combobox i added ToList() and i got this error: An unhandled exception of type 'System.Data.Entity.Core.EntitySqlException' occurred in EntityFramework.SqlServer.dll Additional information: 'Hats' could not be resolved in the current scope or context. Make sure that all referenced variables are in scope, that required schemas are loaded, and that namespaces are referenced correctly. Near simple identifier, line 1, column 66. – Soundview Dec 13 '16 at 10:46
  • In your example, you are concatenating the SQL string. You should use the parameters to `CreateQuery` to add `filterCombobox.SelectedValue` as a filter – Andrés Robinet Dec 13 '16 at 15:46
  • How would I be able to do that. I'm sorry I'm very new to data access and entity framework. I've been using c# for two weeks only. – Soundview Dec 13 '16 at 18:30
  • There's an example [at the end of this doc] (https://msdn.microsoft.com/en-us/library/bb339670(v=vs.110).aspx). Your object parameter value would be `filterCombobox.SelectedValue` – Andrés Robinet Dec 13 '16 at 18:39
  • `ObjectContext context = ((IObjectContextAdapter)db).ObjectContext; string queryString =@"SELECT VALUE c FROM Product AS c WHERE c.ProductType = " + comboBox1.SelectedValue; ObjectQuery productQuery = context.CreateQuery(queryString, comboBox1.SelectedValue);` I am having trouble passing combobox.selectedvalue as a parameter – Soundview Dec 13 '16 at 18:59
  • How can I pass the 'filterComboBox.SelectedValue' as a parameter. The example from msdn is passing strings as parameter? Any help please? Thx – Soundview Dec 14 '16 at 12:26