English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

وظائف التحويل LINQ

يمكن استخدام محولات العمليات في LINQ لتحويل نوع عناصر السلسلة (المجموعة). يتم تقسيم محولات العمليات إلى ثلاثة أنواع:Asالعمليات (AsEnumerable و AsQueryable)،Toالعمليات (ToArray، ToDictionary، ToList و ToLookup) والتحويلالعمليات (Cast و OfType)

الجدول أدناه يبين جميع محولات العمليات.

الطريقةالوصف
AsEnumerable

إرجاع السلسلة ك IEnumerable<T>

AsQueryable

AsQueryable

تحويل

تحويل مجموعة غير عامة إلى مجموعة عامة (IEnumerable إلى IEnumerable)

OfTypeتصفية المجموعة بناءً على نوع معين
ToArrayتحويل المجموعة إلى مصفوفة
ToDictionary

وضع العناصر في Dictionary بناءً على دالة اختيار المفتاح

ToList

تحويل المجموعة إلى List

ToLookupالجمع بين العناصر في Lookup<TKey, TElement>

AsEnumerable و AsQueryable methods

تقوم طرق AsEnumerable و AsQueryable بتحويل أو تحويل العنصر المصدر إلى IEnumerable <T> أو IQueryable <T>.

انظر إلى المثال التالي:

class Program
{
    static void ReportTypeProperties<T>(T obj)
    {
        Console.WriteLine("Compile-time type: {0}", typeof(T).Name);
        Console.WriteLine("Actual type: {0}", obj.GetType().Name);
    }
    static void Main(string[] args)
    {
        Student[] studentArray = { 
                new Student() { StudentID = 1, StudentName = "John", Age = 18 } ,
                new Student() { StudentID = 2, StudentName = "Steve", Age = 21 } ,
                new Student() { StudentID = 3, StudentName = "Bill", Age = 25 } ,
                new Student() { StudentID = 4, StudentName = "Ram", Age = 20 } ,
                new Student() { StudentID = 5, StudentName = "Ron" , Age = 31 } ,
            };   
            
        ReportTypeProperties( studentArray);
        ReportTypeProperties(studentArray.AsEnumerable());
        ReportTypeProperties(studentArray.AsQueryable());   
    }
}
الإخراج:
نوع الفعل في وقت التجميع: Student[]
نوع الفعل الحقيقي: Student[]
نوع الفعل في وقت التجميع: IEnumerable`1
نوع الفعل الحقيقي: Student[]
Compile-time type: IQueryable`1
Actual type: EnumerableQuery`1

كما هو موضح في المثال السابق، يقوم AsEnumerable و AsQueryable بتحويل نوع التجميع في وقت التجميع إلى IEnumerable و IQueryable

تحويل

يؤدي محول التحويل إلى نفس الفعل كما يفعل AsEnumerable<T>. إنه يحول العنصر المصدر إلى IEnumerable<T>.

class Program
{
    static void ReportTypeProperties<T>(T obj)
    {
        Console.WriteLine("Compile-time type: {0}", typeof(T).Name);
        Console.WriteLine("Actual type: {0}", obj.GetType().Name);
    }
    static void Main(string[] args)
    {
        Student[] studentArray = { 
                new Student() { StudentID = 1, StudentName = "John", Age = 18 } ,
                new Student() { StudentID = 2, StudentName = "Steve", Age = 21 } ,
                new Student() { StudentID = 3, StudentName = "Bill", Age = 25 } ,
                new Student() { StudentID = 4, StudentName = "Ram", Age = 20 } ,
                new Student() { StudentID = 5, StudentName = "Ron" , Age = 31 } ,
            };   
         
        ReportTypeProperties( studentArray);
        ReportTypeProperties(studentArray.Cast<Student>());
    }
}
الإخراج:
نوع الفعل في وقت التجميع: Student[]
نوع الفعل الحقيقي: Student[]
نوع الفعل في وقت التجميع: IEnumerable`1
نوع الفعل الحقيقي: Student[]
نوع الفعل في وقت التجميع: IEnumerable`1
نوع الفعل الحقيقي: Student[]
نوع الفعل في وقت التجميع: IEnumerable`1
نوع الفعل الحقيقي: Student[]

studentArray.Cast<Student>() هو نفسه كما (IEnumerable<Student>)studentArray، ولكن Cast<Student>() أكثر وضوحًا.

عمليات To: ToArray()، ToList()، ToDictionary()

كما يوحي الاسم، يتحول ToArray()، ToList()، ToDictionary() من قبل الأجسام الأصلية إلى مصفوفة، قائمة أو قاموس.

عمليات To إلزامية للبحث. إنها إلزامية على مزود البحث البعيد تنفيذ البحث واستخراج النتائج من المصدر الأساسي (مثل قاعدة بيانات SQL Server).

IList<string> strList = new List<string>() { 
                                            "واحد", 
                                            "اثنان", 
                                            "ثلاثة", 
                                            "أربعة", 
                                            "ثلاثة" 
                                            };
string[] strArray = strList.ToArray<string>();// تحويل القائمة إلى مصفوفة
IList<string> list = strArray.ToList<string>(); // تحويل المصفوفة إلى قائمة

ToDictionary - تحويل قائمة جينيريك إلى قاموس جينيريك:

IList<Student> studentList = new List<Student>() { 
                    new Student() { StudentID = 1, StudentName = "John", age = 18 } ,
                    new Student() { StudentID = 2, StudentName = "Steve", age = 21 } ,
                    new Student() { StudentID = 3, StudentName = "Bill", age = 18 }
                    new Student() { StudentID = 4, StudentName = "Ram", age = 20 }
                    new Student() { StudentID = 5, StudentName = "Ron", age = 21 } 
                };
//سيعتمد التحويل التالي القائمة إلى قاموس، حيث يكون StudentId هو المفتاح
IDictionary<int, Student> studentDict = 
                                studentList.ToDictionary<Student, int>(s => s.StudentID); 
foreach(var key in studentDict.Keys)
	Console.WriteLine("المفتاح: {0}, القيمة: {1}", 
                                key, (studentDict[key] as Student).StudentName);
الإخراج:
المفتاح: 1، القيمة: John
المفتاح: 2، القيمة: Steve
المفتاح: 3، القيمة: Bill
المفتاح: 4، القيمة: Ram
المفتاح: 5، القيمة: Ron

الشكل التالي يوضح كيف أن studentDict يحتوي على زوج مفتاح-قيمة، حيث يكون المفتاح هو StudentID، والقيمة هي كائن Student.

وظائف LINQ-ToDictionary