ObjectQuery<T> 构造函数
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
初始化 ObjectQuery<T> 类的新实例。
重载
| 名称 | 说明 |
|---|---|
| ObjectQuery<T>(String, ObjectContext) |
使用指定的 Entity SQL 命令作为初始查询创建一个新 ObjectQuery<T> 实例。 |
| ObjectQuery<T>(String, ObjectContext, MergeOption) |
使用指定的 Entity SQL 命令作为初始查询和指定的合并选项创建一个新 ObjectQuery<T> 实例。 |
注解
可以通过这样一种方式初始化一 ObjectQuery<T> 个标量结果,而不是标量结果的集合。 某些扩展方法需要收集结果作为输入。 在这种情况下,当调用其中一个方法时,将引发一 ArgumentException 个。 有关详细信息,请参阅对象查询。
当应用程序在运行时生成实体 SQL 查询时,应注意数据源的任何命令长度限制。 实体 SQL 不会对查询中的命令文本长度实施限制。
ObjectQuery<T>(String, ObjectContext)
使用指定的 Entity SQL 命令作为初始查询创建一个新 ObjectQuery<T> 实例。
public:
ObjectQuery(System::String ^ commandText, System::Data::Objects::ObjectContext ^ context);
public ObjectQuery(string commandText, System.Data.Objects.ObjectContext context);
new System.Data.Objects.ObjectQuery<'T> : string * System.Data.Objects.ObjectContext -> System.Data.Objects.ObjectQuery<'T>
Public Sub New (commandText As String, context As ObjectContext)
参数
- commandText
- String
实体 SQL 查询。
- context
- ObjectContext
执行 ObjectContext 查询的依据。
示例
此示例演示如何构造类的 ObjectQuery<T> 实例。
using (AdventureWorksEntities context =
new AdventureWorksEntities())
{
// Call the constructor with a query for products and the ObjectContext.
ObjectQuery<Product> productQuery1 =
new ObjectQuery<Product>("Products", context);
foreach (Product result in productQuery1)
Console.WriteLine("Product Name: {0}", result.Name);
string queryString =
@"SELECT VALUE product FROM AdventureWorksEntities.Products AS product";
// Call the constructor with the specified query and the ObjectContext.
ObjectQuery<Product> productQuery2 =
new ObjectQuery<Product>(queryString, context);
foreach (Product result in productQuery2)
Console.WriteLine("Product Name: {0}", result.Name);
// Call the constructor with the specified query, the ObjectContext,
// and the NoTracking merge option.
ObjectQuery<Product> productQuery3 =
new ObjectQuery<Product>(queryString,
context, MergeOption.NoTracking);
foreach (Product result in productQuery3)
Console.WriteLine("Product Name: {0}", result.Name);
}
注解
当应用程序在运行时生成实体 SQL 查询时,应注意数据源的任何命令长度限制。 实体 SQL 不会对查询中的命令文本长度实施限制。
另请参阅
适用于
ObjectQuery<T>(String, ObjectContext, MergeOption)
使用指定的 Entity SQL 命令作为初始查询和指定的合并选项创建一个新 ObjectQuery<T> 实例。
public:
ObjectQuery(System::String ^ commandText, System::Data::Objects::ObjectContext ^ context, System::Data::Objects::MergeOption mergeOption);
public ObjectQuery(string commandText, System.Data.Objects.ObjectContext context, System.Data.Objects.MergeOption mergeOption);
new System.Data.Objects.ObjectQuery<'T> : string * System.Data.Objects.ObjectContext * System.Data.Objects.MergeOption -> System.Data.Objects.ObjectQuery<'T>
Public Sub New (commandText As String, context As ObjectContext, mergeOption As MergeOption)
参数
- commandText
- String
实体 SQL 查询。
- context
- ObjectContext
执行 ObjectContext 查询的依据。
- mergeOption
- MergeOption
指定如何将此查询检索到的实体与以前针对同 ObjectContext一查询返回的实体合并。
示例
在此示例中,使用 ObjectQuery<T> 指定的查询 ObjectContext初始化 ,以及 MergeOption。
int productID = 900;
using (AdventureWorksEntities context =
new AdventureWorksEntities())
{
string queryString = @"SELECT VALUE product FROM
AdventureWorksEntities.Products AS product
WHERE product.ProductID > @productID";
ObjectQuery<Product> productQuery1 =
new ObjectQuery<Product>(queryString,
context, MergeOption.NoTracking);
productQuery1.Parameters.Add(new ObjectParameter("productID", productID));
ObjectQuery<DbDataRecord> productQuery2 =
productQuery1.Select("it.ProductID");
foreach (DbDataRecord result in productQuery2)
{
Console.WriteLine("{0}", result["ProductID"]);
}
}
注解
当应用程序在运行时生成实体 SQL 查询时,应注意数据源的任何命令长度限制。 实体 SQL 不会对查询中的命令文本长度实施限制。