ObjectQuery<T>.Distinct 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
将查询限制为唯一结果。
public:
System::Data::Objects::ObjectQuery<T> ^ Distinct();
public System.Data.Objects.ObjectQuery<T> Distinct ();
member this.Distinct : unit -> System.Data.Objects.ObjectQuery<'T>
Public Function Distinct () As ObjectQuery(Of T)
返回
一个新的 ObjectQuery<T> 实例,等效于应用了 SELECT DISTINCT 的原始实例。
示例
此示例使用 UnionAll 方法创建新 ObjectQuery<T> 对象。 然后对新的 Distinct 对象调用 ObjectQuery<T>,以获取此查询的唯一结果。
int productID = 100;
using (AdventureWorksEntities context =
new AdventureWorksEntities())
{
string queryString =
@"SELECT VALUE product FROM AdventureWorksEntities.Products
AS product WHERE product.ProductID < @productID";
ObjectQuery<Product> productQuery =
new ObjectQuery<Product>(queryString,
context, MergeOption.NoTracking);
ObjectQuery<Product> productQuery2 =
new ObjectQuery<Product>(queryString,
context, MergeOption.NoTracking);
ObjectQuery<Product> productQuery3 =
productQuery.UnionAll(productQuery2);
productQuery3.Parameters.Add(new ObjectParameter("productID", productID));
Console.WriteLine("Result of UnionAll");
Console.WriteLine("------------------");
// Iterate through the collection of Product items,
// after the UnionAll method was called on two queries.
foreach (Product result in productQuery3)
{
Console.WriteLine("Product Name: {0}", result.ProductID);
}
ObjectQuery<Product> productQuery4 = productQuery3.Distinct();
Console.WriteLine("\nResult of Distinct");
Console.WriteLine("------------------");
// Iterate through the collection of Product items.
// after the Distinct method was called on a query.
foreach (Product result in productQuery4)
Console.WriteLine("Product Name: {0}", result.ProductID);
}
注解
此查询生成器方法返回一个 ObjectQuery<T> 实例,该实例等效于应用 了 SELECT DISTINCT 的原始查询。
运算符 DISTINCT
不能应用于包含到数据源 (中非可比列的映射的对象,例如 ntext) 。