掲示板システム
ホーム
アクセス解析
カテゴリ
ログアウト
List(Of 構造体)の構造体の指定したメンバに特定の値が含まれているかどうか調べるには? (ID:146111)
名前
ホームページ(ブログ、Twitterなど)のURL (省略可)
本文
'Dim srcList As New List(Of Stru1)() Dim targetValue As String = "hogehoge" '≪A:該当する Stru1 を 1 つ取得≫ 'Find メソッド ' 複数該当する場合は、最初に一致した Stru1 構造体。 ' 該当項目が無い時は、空の Stru1 構造体。 Dim dstFirstStru1 As Stru1 = srcList.Find(Function(x) x.Value2 = targetValue) 'FindLast メソッド ' 複数該当する場合は、最後に一致する Stru1 構造体。 ' 該当項目が無い時は、空の Stru1 構造体。 Dim dstLastStru1 As Stru1 = srcList.FindLast(Function(x) x.Value2 = targetValue) '≪B:該当する Stru1 をすべて取得≫ 'FindAll メソッド Dim dstList As List(Of Stru1) = srcList.FindAll(Function(x) x.Value2 = targetValue) '≪C:該当する Index を 1 つ取得≫ 'FindIndex メソッド ' 複数該当する場合は、最初に一致した Index。 ' 該当項目が無い時は、-1 。 Dim dstFirstIndex As Integer = srcList.FindIndex(Function(x) x.Value2 = targetValue) 'FindLastIndex メソッド ' 複数該当する場合は、最後に一致する Index。 ' 該当項目が無い時は、-1 。 Dim dstLastIndex As Integer = srcList.FindLastIndex(Function(x) x.Value2 = targetValue) '≪D:該当する Index をすべて取得≫ 'ループ処理以外では、こんな方法しか思いつかず…。 Dim dstAllIndices() As Integer = (From x In srcList.Select(Function(y, i) New With {y, i}) Where x.y.Value2 = targetValue Select x.i).ToArray() '≪E:該当する物があるかどうかを判定≫ 'Exists メソッド、または Any 拡張メソッド Dim hasData As Boolean = srcList.Exists(Function(x) x.Value2 = targetValue) '≪F:すべての項目が該当しているかどうかを判定≫ 'All 拡張メソッド Dim isAll As Boolean = srcList.All(Function(x) x.Value2 = targetValue)
←解決時は質問者本人がここをチェックしてください。
戻る
掲示板システム
Copyright 2020 Takeshi Okamoto All Rights Reserved.