私はデータベースから派生した次のEFクラスを持っています(簡略化)
class Product
{
public string ProductId;
public string ProductName;
public string CategoryId;
public string CategoryName;
}
ProductId
テーブルの主キーです。
DBデザイナによって行われた悪い設計決定(私はそれを変更することはできません)のために、私はこの表にCategoryId
あります。CategoryName
(distinct)をValueおよびTextとして持つDropDownListが必要です。したがって、次のコードを適用しました。CategoryId
CategoryName
product.Select(m => new {m.CategoryId, m.CategoryName}).Distinct();
CategoryId
論理的には、プロパティとしておよびを使用して匿名オブジェクトを作成する必要がありCategoryName
ます。重複ペア( 、 )Distinct()
がないことを保証します。 CategoryId
CategoryName
しかし、実際には機能しません。コレクションにフィールドが1つしかない場合の作品を理解している限り、Distinct()
それ以外の場合は無視されます...正しいですか?回避策はありますか?ありがとう!
アップデート
申し訳ありませんproduct
が:
List<Product> product = new List<Product>();
私は同じ結果を得る別の方法を見つけましたDistinct()
:
product.GroupBy(d => new {d.CategoryId, d.CategoryName})
.Select(m => new {m.Key.CategoryId, m.Key.CategoryName})