الفريق العربي للبرمجةأرشيف المنتديات · 2000 – 2023
نسخة أرشيفية للقراءة فقط — التسجيل والمشاركة مغلقان، والمحتوى محفوظ كما كان.

الحصول على قيم حقل من قاءمة ابجكه و كتابته في string

بدأه زكيري عبد العالي في 6 يوليو 2012 · 4 رد · 594 مشاهدة · في Microsoft Visual C#.NET
مشاركة: واتساب X فيسبوك تيليجرام
#1 صاحب الموضوع

السلام عليكم

عندي سؤال حول فلترة قاءمة عن طريق linq

public class CMetric
    {
       public int ID { get; set; }
public string Name { get; set; }
..
..
..
    }

public class CProject
    {
       public int ID { get; set; }
       public string Name { get; set; }
..
..
..
..
        public List<CMetric> Metrics { get; set; }

مثال
1	Project1    
•	1 	Metric1
•	2	Metric2 
•	3 	Metric3

المطلوب
الحصول على قاءمة   Metric ID 
metricsList = ‘1’,’2’,’3’…

List<int> list = new List<int>();
foreach (CMetric Metric in project.Metrics)
{
    list.Add(Metric.ID);
}

string metricsList = String.Join(",", list.ToArray());

شكرا

طبعا لا اريد عمل حلقة

#2
static void Main(string[] args)
        {
            List<int> list = new List<int>();
            CProject project = new CProject();
            project.Metrics = new List<CMetric>();
            project.Metrics.Add(new CMetric(1,"a"));
            project.Metrics.Add(new CMetric(2, "b"));
            project.Metrics.Add(new CMetric(3, "c"));
            foreach (CMetric Metric in project.Metrics)
            { 
                list.Add(Metric.ID); 
            }
            // For example get me all Metrics with ID lager than 2 
            string metricsList = String.Join(",", list.Where(item => item > 2));

        }

    }
    public class CMetric { 
        public CMetric(int id, string name) 
        {
            ID = id;
            Name = name;
        }
        public int ID { get; set; } public string Name { get; set; } }
    public class CProject
    {
        public int ID { get; set; }
        public string Name { get; set; }
        public List<CMetric> Metrics { get; set; }
    }

ENG. IMAD, Programing is a way of life, It's in the air, we breath It's pumps through our veines, Without it, we soon crumble to dust

#3

شكرا على الرد

ربما سؤال غير واضح شارح اكثر

شكرا على الرد

اريد ان استرجع سلسلة بالشكل التالي

metricsList = ‘1’,’2’,’3’…

اي استبدال كود هده الحلقة بواسطة select in t from .....

List<int> list = new List<int>();
foreach (CMetric Metric in project.Metrics)
{
list.Add(Metric.ID);
}

ثم احول الناتج الى string

string metricsList = String.Join(",", list.ToArray());

القاءمة هي حقل من الابجكت Project

Project.Metrics

و فيها نجد مثلا

NAME Filed3 ID

11 Name1 X1

22 Name2 X2

33 Name3 X3

المطلب استرجاع string لكل قيم ID

string metricsIDList = '11','22','33' ....

شكرا على لاهتمام

ضننت انه يمكن كتابت سطر linq

مثلا

var list = from i in Project.Metrics select i.ID

عوض عمل الحلقة foreach

#4
string metricsList = String.Join(",", project.Metrics.Select(item => item.ID));

All of the code

static void Main(string[] args)
        {

             CProject project = new CProject();
            project.Metrics = new List<CMetric>();
            project.Metrics.Add(new CMetric(1,"a"));
            project.Metrics.Add(new CMetric(2, "b"));
            project.Metrics.Add(new CMetric(3, "c"));

            string metricsList = String.Join(",", project.Metrics.Select(item => item.ID));
        }

    }
    public class CMetric { 
        public CMetric(int id, string name) 
        {
            ID = id;
            Name = name;
        }
        public int ID { get; set; } public string Name { get; set; } }
    public class CProject
    {
        public int ID { get; set; }
        public string Name { get; set; }
        public List<CMetric> Metrics { get; set; }
    }

تم تعديل هذه المشاركة بواسطة Super_emad55 في 7 يوليو 2012 في 12:14

ENG. IMAD, Programing is a way of life, It's in the air, we breath It's pumps through our veines, Without it, we soon crumble to dust

#5

و هو المطلوب بارك الله فيك

مواضيع مشابهة