This topic shows how to execute an Entity SQL query with parameters using ObjectQuery.
I will replace my previous article Querying with Entity SQL example with parameters using ObjectQuery.
Use the following namespace.
using System.Data.Objects;
Sample code:
private void button1_Click(object sender, EventArgs e)
{
using (var context = new AdventureWorksEntities1())
{
string query =
"SELECT VALUE c FROM Contact AS c " +
"WHERE c.LastName=@LastName";
var contacts = new ObjectQuery<Contact>(query, context);
contacts.Parameters.Add(new ObjectParameter("LastName", "Clark"));
dataGridView1.DataSource = contacts.ToList();
}
The above query will return all the contacts that have lastname equal to Clark.