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) 。