ObjectParameterCollection.Add Method (ObjectParameter)


The .NET API Reference documentation has a new home. Visit the .NET API Browser on docs.microsoft.com to see the new experience.

Adds the specified ObjectParameter to the collection.

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

public void Add(
	ObjectParameter parameter


Type: System.Data.Objects.ObjectParameter

The parameter to add to the collection.

Exception Condition

The parameter argument is null.


The parameter argument already exists in the collection. This behavior differs from that of most collections that allow duplicate entries.


Another parameter with the same name as the parameter argument already exists in the collection. Note that the lookup is case-insensitive. This behavior differs from that of most collections, and is more like that of a T:System.Collections.Generic.Dictionary.


The type of the parameter is not valid.

After parameters have been added, they can be removed from the collection and the collection can be cleared, as long as the query has not been compiled or executed. Parameter names cannot be changed, but values can be changed at any time.

Parameters must be unique in the ObjectParameterCollection. There cannot be two parameters in the collection with the same name. For more information, see Query Builder Methods.

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

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

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

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

    ObjectParameterCollection objectParameterCollection =

    Console.WriteLine("Count is {0}.", objectParameterCollection.Count);

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

.NET Framework
Available since 3.5
Return to top