私はC#とLinq-to-Sqlを初めて使用します。
この形式のテーブル「InstrumentTypes」があります。
typeId(int) | type(varchar) | subttype(varchar)
101 Keys keyboard
102 Keys accessories
103 Guitar acoustic
104 Guitar electric
入力として「type」による検索に基づいてテーブルからすべての「typeId」をフェッチする必要があり、すべてのtypeIdをASPリピーターにバインドする必要があります。
これまでのところ、次のコードを記述しました。
// requestType contains the type from the search
var type = (from m in database.InstrumentTypes
where m.type == requestType
select m);
foreach(var typeId in type)
{
//code
}
クエリの結果を反復処理してデータ構造に格納し、リピーターにバインドする方法を理解できません。
次のコードは、それをリピーターにバインドします。
Repeater1.DataSource= //name of data structure used to store the types goes here
Repeater1.DataBind();
誰か助けてくれませんか?
編集:取得したtypeIDごとに、別のテーブル「Instruments」にアクセスして、そのtypeIdに属するすべてのInstrumentsを取得したいと思います。テーブル「Instruments」は次のようになります。
instrumentId typeID name description
1000 101 yamaha xyz
アリアルドの答えに基づいて、私はこれを行っています:
var type = (from m in database.InstrumentTypes
where m.type == requestType
select m);
var instruments = new List<Instrument>();
foreach (var i in type)
{
instruments.Add(from x in database.Instruments
where x.typeId == i.typeId
select x);
}
Repeater1.DataSource = instruments;
Repeater1.DataBind();
しかし、「リストに最適なオーバーロードされたメソッドの一致にはいくつかの無効な引数があります」というコンパイルエラーが発生します。どこが間違っているのですか?