に囲まれているにCheckedListBoxを検索:私はデータテーブルにバインドされたデータとにCheckedListBox(リサイズ)を持っているデータテーブル使用してLINQ
clbCustomer.DataSource = ds.Tables["Default"];
clbCustomer.DisplayMember = "desc";
clbCustomer.ValueMember = "customerId";
今、私は特定の顧客IDのにCheckedListBoxを検索したいですその行を選択します。私は次のようにforeachステートメントでこれを行うことができます:
// Find the index
int index = 0;
foreach (DataRowView item in clbCustomer.Items)
{
int cusId = Convert.ToInt32(item["customerId"]);
if (cusId == 255)
{
break;
}
index++;
}
// Select the customer
clbCustomer.SetItemChecked(index, true);
ただし、このようにするのは非常に嵩張っているようです。上記のコードをlinqに変換しようとしていますが、それを達成できませんでした。これは私がこれまで持っているものです:
// Find the index (not working)
int index = clbCustomer.Items.Cast<DataRowView>().Where(x => x["customerId"] == 255);
// Select the customer
clbCustomer.SetItemChecked(index, true);
しかし、linqを使用してその顧客IDのインデックスを抽出する方法がわかりません。どんな助けもありがとう。ありがとう。下にKeithin8aによって提供さ
ソリューション:コメントで述べたように、そのような
var item = clbCustomer.Items.Cast<DataRowView>().Where(x => Convert.ToInt32(x["customerId"]) == 255).FirstOrDefault();
int index = clbCustomer.Items.IndexOf(item);
この行は次のようになります:int index = clbCustomer.Items.Cast().Where(x => x ["customerId"] == 255);まったくコンパイルできますか?ここに返された結果はコレクションだと思われます。だから、FirstorDefaultを追加して修正できますか?私が間違っているなら、私を訂正してください。 –
ありがとうございます。いいえ、その行はまったく機能しません。それは私がどのように修正するかわからないものです。 –
var index = clbCustomer.Items.Cast().Where(x => x ["customerId"] == 255).FirstOrDefaut(); clbCustomer.SetItemChecked(index.customerId、true);おそらく役に立ちます=) –