Edit

Share via


ObjectParameterCollection.Add(ObjectParameter) Method

Definition

Adds the specified ObjectParameter to the collection.

public:
 virtual void Add(System::Data::Objects::ObjectParameter ^ parameter);
public void Add (System.Data.Objects.ObjectParameter parameter);
abstract member Add : System.Data.Objects.ObjectParameter -> unit
override this.Add : System.Data.Objects.ObjectParameter -> unit
Public Sub Add (parameter As ObjectParameter)

Parameters

parameter
ObjectParameter

The parameter to add to the collection.

Implements

Exceptions

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.

-or-

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 Dictionary<TKey,TValue>.

The type of the parameter is not valid.

Examples

This 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 =
        contactQuery.Parameters;

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

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

Remarks

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.

Applies to

See also