Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/256.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 方法链接泛型列表扩展_C#_Generics_.net 2.0_Findall_Convertall - Fatal编程技术网

C# 方法链接泛型列表扩展

C# 方法链接泛型列表扩展,c#,generics,.net-2.0,findall,convertall,C#,Generics,.net 2.0,Findall,Convertall,我有一个“复杂”类型的列表——一个具有一些字符串属性的对象。列表本身是另一个对象的属性,包含各种类型的对象,如此缩写类结构所示: Customer { public List<Characteristic> Characteristics; . . . } Characteristic { public string CharacteristicType; public string CharacteristicValue; } 客户{ 公共列

我有一个“复杂”类型的列表——一个具有一些字符串属性的对象。列表本身是另一个对象的属性,包含各种类型的对象,如此缩写类结构所示:

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

Characteristic {
   public string CharacteristicType;
   public string CharacteristicValue;
}
客户{
公共列表特征;
.
.
.
}
特色{
公共字符串特征类型;
公共字符串特征值;
}
我希望能够为当前客户收集给定类型特征的值列表,我可以通过以下两步流程完成:

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;
   }
);
List interestCharacteristics=customer.Characteristics.FindAll(
代表(特殊利益){
返回利息。CharacteristicType==“利息”;
}
);
列表兴趣=兴趣特征.ConvertAll(
代表(特殊利益){
返回利息。特征值;
}
);
这很好,但似乎还有很长的路要走。我确信我一定错过了一个更简单的方法来获取这个列表,或者将FindAll()和Convert()方法链接在一起,或者其他我完全忽略的方法

作为背景,我在.NET2.0中工作,所以我仅限于.NET2泛型,而Characteristic类是一个外部依赖项-我无法改变它的结构来简化它,类的其他方面也很重要,只是与这个问题无关


欢迎任何指针或附加阅读。

为什么不创建一个
字典
,这样您就可以添加“兴趣”作为键,并添加一个值列表作为值。例如:

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 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");
公共字典特性;
..
添加(CharacteristicType.Interest,new List());
特征[CharacteristicType.Interest]。添加(“在StackOverflow上发布问题”);
特征[CharacteristicType.Interest]。添加(“回答有关StackOverflow的问题”);

我会手工做一些工作。通过先进行FindAll,然后进行Convert,可以在集合中循环两次。这似乎没有必要。如果在一天结束时,您只需要一个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);
        }
     }
谓词条件=委托(特征兴趣)
{
返回利息。CharacteristicType==“利息”;
};
List myList=新列表();
foreach(客户特征中的特征c)
{
如果(标准(c))
{
myList.Add(c.特征值);
}
}

下面是一个生成器实现

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

关于使用枚举作为键的一个警告:如果使用枚举作为字典键,可能需要为其编写IEqualityComparer的实现,以避免不必要的装箱。欲了解更多信息(无耻插头):
public static IEnumerable<string> GetInterests(Customer customer)
{
    foreach (Characteristic c in customer.Characteristics)
    {
        if (c.CharacteristicType == "Interest")
            yield return c.CharacteristicValue;
    }
}
customer.Characteristics
    .Where(c => c.CharacteristicType == "Interest")
    .Select(c => c. CharacteristicValue);