2010-11-25 12 views
0

Linqのクエリは次のとおりです。Linqエラー:グループ化できません

Common.ProductType.ProductTypeEnum[] aCustomerProduct = 
    (from customer in repositoryCustomer.GetAll().Where(cp => cp.Email == strEmail) 
    join cp in repositoryCustomerProduct.GetAll() on customer.CustomerId equals cp.CustomerId 
    select cp.ProdId 
    ).ToArray<ProductType.ProductTypeEnum>(); 

すべて正常に動作しています.4つのアイテムの配列が返されます。

しかし、ここで私はユニークなアイテムだけを受け取る必要があるので、ここでグループを追加しました:

Common.ProductType.ProductTypeEnum[] aCustomerProduct = 
    (from customer in repositoryCustomer.GetAll().Where(cp => cp.Email == strEmail) 
    join cp in repositoryCustomerProduct.GetAll() on customer.CustomerId equals cp.CustomerId 
    group cp by cp.ProdId into products0 
    select products0.Key 
    ).ToArray<ProductType.ProductTypeEnum>(); 

そして、配列の代わりに私はエラーメッセージを受け取りました:

'値'は間違ったタイプでした。 'Common.ProductType + ProductTypeEnum'が必要です。実際の 'System.Int32'。

2番目のクエリで何が問題になっていますか?

助けてください。どうもありがとう!

P.S. 2番目のクエリはMSSQLサーバにはまったく行きません(Profilerは何も表示しません)。

答えて

1

なぜあなたは一意性のためだけにグループ化していますか?なぜ、別のものを使用しないのですか?

(from **blah 
join **blaa 
select **blah).Distinct().ToArray(); 

これにより、より効率的なSQLが生成されます。

+0

それに同意します。どうもありがとう! – Budda

1

ProdIdをキーとしてグループ化し、キーを選択しました。したがって、ProdIdが整数の場合、クエリはIEnumerable<int>を返します。暗黙的にタイプProductType.ProductTypeEnumの配列に変更することはできません。

+0

私のリポジトリクラスのメソッド 'GetAll()'はオブジェクトを返し、CustomerProductオブジェクトの 'ProdId'フィールドは 'Common.ProductType.ProductTypeEnum'型です。これはグループ化を妨げないはずです...しかし...おそらくあなたは正しいです:グループ化はSQLサーバ側で行われ、ProdIdは本当に整数です...ありがとう。チェックします。 – Budda

+0

...実際には、Linqはproducts0.Keyが実際には 'Common.ProductType.ProductTypeEnum'タイプのものだと教えてくれます... – Budda

関連する問題