Share via


ObjectParameterCollection.Add(ObjectParameter) 方法

定义

将指定的 ObjectParameter 添加到集合。

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)

参数

parameter
ObjectParameter

要添加到集合的参数。

实现

例外

parameter 参数为 null

parameter 实参已存在于集合中。 此行为与允许重复项的大多数集合的行为不同。

- 或 -

parameter 实参具有相同名称的另一个形参已存在于集合中。 注意,查找是区分大小写的。 此行为与大多数集合的行为不同,更类似于 Dictionary<TKey,TValue> 的行为。

parameter 的类型无效。

示例

本示例将新参数添加到 集合。 它循环访问 ObjectParameterCollection 并显示集合中每个参数的名称、类型和值。

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

注解

添加参数后,只要还没有编译或执行查询,就可以从集合中移除参数,并且可以清除集合。 不能更改参数名称,但可以随时对值进行更改。

参数在 ObjectParameterCollection 中必须是唯一的。 集合中不能有两个具有相同名称的参数。 有关详细信息,请参阅 查询生成器方法

适用于

另请参阅