سؤال

ولدي قائمة من نوع "معقدة" - كائن مع بعض خصائص السلسلة. القائمة نفسها هي خاصية كائن آخر ويحتوي على كائنات من مجموعة متنوعة من أنواع، كما هو مبين في هذه البنية الطبقية مختصرة:

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 ()، وتحويل () طرق، أو أي شيء آخر وأنا تطل تماما.

لالخلفية، وأنا أعمل في صافي 2.0، لذلك أنا يقتصر على الأدوية صافي 2، والطبقة المميزة هو تبعية خارجية - لا أستطيع تغييره من هيكل لتبسيط ذلك، وهناك جوانب أخرى من الفئة التي تعتبر مهمة، ليس فقط في العلاقات لهذه المشكلة.

ورحبت أي مؤشرات أو قراءة إضافية.

هل كانت مفيدة؟

المحلول

إليك تطبيق مولد

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);

نصائح أخرى

وأود أن تفعل بعض manualy العمل. عن طريق القيام FindAll أولا، ومن ثم تحويل، كنت حلقات من خلال مجموعتك مرتين. لا يبدو الضروره. إذا كان كل ما تريد في نهاية المطاف، هو قائمة 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);
        }
     }

لماذا لا ننشئ Dictionary<string, List<string>>، وبهذه الطريقة يمكنك إضافة "الفائدة" كمفتاح، وقائمة القيم حيث بلغت قيمة. على سبيل المثال:

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");
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top