This documentation is archived and is not being maintained.

ObjectParameter Constructor (String, Object)

Initializes a new instance of the ObjectParameter class with the specified name and value.

Namespace:  System.Data.Objects
Assembly:  System.Data.Entity (in System.Data.Entity.dll)

public ObjectParameter(
	string name,
	Object value


Type: System.String

The parameter name. This name should not include the "@" parameter marker that is used in Entity SQL statements, only the actual name. The first character of the expression must be a letter. Any successive characters in the expression must be either letters, numbers, or an underscore (_) character.

Type: System.Object

The initial value (and inherently, the type) of the parameter.


If the value of either argument is null.


If the value of the name argument is not valid. Parameter names must start with a letter and can only contain letters, numbers, and underscores.

This constructor creates a parameter from the specified name and value. The type of the parameter is inferred from the value.

When added to the ObjectParameterCollection, the parameter name cannot be changed. The parameter value can be changed through the Value property. After the query has been compiled, the value cannot be changed. For more information, see Query Builder Methods (Entity Framework).

The example in this topic is based on the Adventure Works Sales Model. The example adds new parameters to the collection. It iterates through the ObjectParameterCollection and displays the name, the type and the value of each parameter in the collection.

using (AdventureWorksEntities advWorksContext =
    new AdventureWorksEntities())
        string queryString =
            @"SELECT VALUE Contact FROM AdventureWorksEntities.Contact 
            AS Contact WHERE Contact.LastName = @ln 
            AND Contact.FirstName = @fn";

        ObjectQuery<Contact> contactQuery =
            new ObjectQuery<Contact>(queryString, advWorksContext);

        // Add parameters to the collection.
        contactQuery.Parameters.Add(new ObjectParameter("ln", "Adams"));
        contactQuery.Parameters.Add(new ObjectParameter("fn", "Frances"));

        ObjectParameterCollection objectParameterCollection = 

        // Iterate through the ObjectParameterCollection. 
        foreach (ObjectParameter result in objectParameterCollection)
            Console.WriteLine("{0} {1} {2}", result.Name,
    catch (EntitySqlException ex)

Windows 7, Windows Vista, Windows XP SP2, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5 SP1