If you find this article useful, consider making a small donation to show your support for this web site and its content.
DiscountASP
AboutMe
About me:
Hi. My name is Farooq Kaiser and I'm a software developer from Toronto, Canada.

Entity SQL query with parameters using ObjectQuery

by Farooq Kaiser 11. June 2009 13:57

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.
 

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags: ,

.NET | C#

Jobs Autos Real estate Videos Power by Google
awesome comments