당신은 CheckedItems
재산 대신 CheckedIndices
사용할 수 있습니다 어쨌든
var selectedTags = this.listView1.CheckedItems
.Cast<ListViewItem>()
.Select(x => x.Tag);
을, 또한 CheckedIndices
는 사용할 수 있습니다 예 :
var selectedTags = this.listView1.CheckedIndices
.Cast<int>()
.Select(i => this.listView1.Items[i].Tag);
편집 : LINQ Select()
의
리틀 설명 :
다음 코드 : 많은에, 날 믿어, 그래서 유용도 코드 길이의 기간의 단축이 특정 예에서
foreach(ListViewItem item in this.listView1.CheckedItems)
{
var tag = item.Tag;
// do some operation using tag
}
되지 않지만 :
var selectedTags = this.listView1.CheckedItems
.Cast<ListViewItem>()
.Select(x => x.Tag);
foreach(var tag in selectedTags)
{
// do some operation using tag
}
는 기능적으로 동일 상황 LINQ 정말 도움이됩니다.
(i) i => 및 (x) x => x.tag를 나타내는 것 – Shahgee
'IEnumerable .Select()'에 람다식이 사용됩니까? 그것은'IEnumerable'을 'Select()'라고하는 다른 IEnumerable에 투영 한 것입니다. 첫 번째는 기본적으로 다음과 같이 말합니다 :'CheckedItems' (x라고 부름)의 각 요소와 각 yield 'x.Tag'를 취하십시오. 그래서 당신은 CheckedItems' '에 해당하는 모든 태그를 포함하는'IEnumerable을
고맙습니다 (희망 ... 영어는 내 모국어가 아닌, 명확하게하기 위해). – Shahgee