1

「複雑な」タイプのリストがあります-いくつかの文字列プロパティを持つオブジェクトです。リスト自体は別のオブジェクトのプロパティであり、次の簡略化されたクラス構造に示されているように、さまざまなタイプのオブジェクトが含まれています。

Customer {
   public List<Characteristic> Characteristics;
   .
   .
   .
}

Characteristic {
   public string CharacteristicType;
   public string CharacteristicValue;
}

現在の顧客の特定のタイプの特性の値のリストを収集できるようにしたいと考えています。これは、次の 2 段階のプロセスで実行できます。

List<Characteristic> interestCharacteristics = customer.Characteristics.FindAll(
   delegate (Characteristic interest) {
      return interest.CharacteristicType == "Interest";
   }
);

List<string> interests = interestCharacteristics.ConvertAll<string>(
   delegate (Characteristic interest) {
      return interest.CharacteristicValue;
   }
);

それはうまく機能しますが、それは長い道のりのようです。FindAll() メソッドと Convert() メソッドを連鎖させるか、完全に見落としている他の方法で、このリストに到達するためのより簡単な方法を見逃しているに違いないと確信しています。

背景として、私は.Net 2.0で作業しているため、.Net 2ジェネリックに限定されており、Characteristicクラスは外部依存関係です.構造を単純化するために変更することはできません.この問題との関係ではなく、重要なクラスです。

任意のポインターまたは追加の読み取りを歓迎します。

4

3 に答える 3

1

私はいくつかの作業を手動で行います。最初にFindAllを実行し、次にConvertを実行することで、コレクションを2回ループします。必要ないようです。1日の終わりに必要なのがCharacteristicValueのリストだけである場合は、元のコレクションをループして、条件に一致する各リストにCharacteristicValueを追加します。このようなもの:

     Predicate<Characteristic> criteria = delegate (Characteristic interest) 
     {
          return interest.CharacteristicType == "Interest";
     };
     List<string> myList = new List<string>();
     foreach(Characteristic c in customer.Characteristics)
     {
        if(criteria(c))
        {
            myList.Add(c.CharacteristicValue);
        }
     }
于 2009-01-26T21:03:07.903 に答える
1

これがジェネレーターの実装です

public static IEnumerable<string> GetInterests(Customer customer)
{
    foreach (Characteristic c in customer.Characteristics)
    {
        if (c.CharacteristicType == "Interest")
            yield return c.CharacteristicValue;
    }
}

残念ながら、要件に基づいて 3.5 拡張メソッドとラムダが提供されていますが、参考までにその方法を次に示します。

customer.Characteristics
    .Where(c => c.CharacteristicType == "Interest")
    .Select(c => c. CharacteristicValue);
于 2009-01-26T22:58:33.210 に答える
0

を作成してみませんか。そうDictionary<string, List<string>>すれば、キーとして「Interest」を追加し、値として値のリストを追加できます。例えば:

Customer {
   public Dictionary<string, List<string>> Characteristics;
   .
   .
   .
}

...

Characteristics.Add("Interest", new List<string>());
Characteristics["Interest"].Add("Post questions on StackOverflow");
Characteristics["Interest"].Add("Answer questions on StackOverflow");
..

List<Characteristic> interestCharacteristics = Characteristics["Interest"];

さらに、必要に応じて、特性を列挙型にすることで可能な値のリストに制限し、それを辞書のキーのデータ型として使用できます。

public enum CharacteristicType
{
   Interest,
   Job,
   ThingsYouHate
//...etc
}

次に、辞書を次のように宣言します。

public Dictionary<CharacteristicType, List<string>> Characteristics;
..
Characteristics.Add(CharacteristicType.Interest, new List<string>());
Characteristics[CharacteristicType.Interest].Add("Post questions on StackOverflow");
Characteristics[CharacteristicType.Interest].Add("Answer questions on StackOverflow");
于 2009-01-26T20:28:13.310 に答える