Export (0) Print
Expand All

ObjectParameterCollection.Add Method

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
)

Parameters

parameter
Type: System.Data.Objects.ObjectParameter
The parameter to add to the collection.

Implements

ICollection<T>.Add(T)

ExceptionCondition
ArgumentNullException

The parameter argument is null.

ArgumentException

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.

ArgumentOutOfRangeException

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 (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, 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);
    }
}


.NET Framework

Supported in: 4, 3.5 SP1

.NET Framework Client Profile

Supported in: 4

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

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

Community Additions

ADD
Show:
© 2014 Microsoft