LINQ ו- AonymousDeligates

מאת גלעד אש
בתאריך 30 אפריל, 2009

בדיקת יכולת ושימוש ב 2 טכניקות חדשות ב visual studio 2008 c# .net 3.5 vs 2008 לגישה וחיתוך נתונים בזמן ריצה: LINQ ו- AonymousDeligates בטיפול ב List Generic של נתוני אוביקטים של סטודנטים המכיל גם מערך ציונים לכל סטודנט, וList Generic של אנשי קשר לסטודנטים שמקושרים באמצעות ה id כולל מיון ראו הסבר מפורט יותר בתוכן המאמר..

LINQ ו- AonymousDeligates

 

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.IO;

using System.Xml;

using System.Xml.Serialization;

 

//consol application

namespace AonymousDeligatesAndLINQ

{

    public class AonymousDeligates

    {

        static void Main(string[] args)

        {

            AonymousDeligates an = new AonymousDeligates();

            an.start();

 

            // Keep the console window open in debug mode

            System.Console.WriteLine("\n\nPress any key to exit.");

            Console.Beep();

            System.Console.ReadKey();

        }

 

        public AonymousDeligates() { }

 

        public void start()

        {

            DAL dal = new DAL();//fill data

            dal.Start();//Linq  and AonymousDeligates

        }

    }

 

    public class DAL

    {

        public DAL() { }

        public void Start()

        {

            DAL app = new DAL();

            //========1  AonymousDeligates.FindAll(T){} == Linq query ???

 

   //==========================================================================

            

 

            #region Linq

            //start linq

 

            // Produce a filtered sequence of unmodified Students.

            IEnumerable<Student> studentQuery1 =

                from student in app.students

                where student.ID > 111

                select student;

 

            Console.WriteLine("Query1: select range_variable");

            foreach (Student s in studentQuery1)

            {

                Console.WriteLine(s.ToString());

            }

 

            // Produce a filtered sequence of elements that contain

            // only one property of each Student.

            IEnumerable<String> studentQuery2 =

                from student in app.students

                where student.ID > 111

                select student.Last;

 

            Console.WriteLine("\r\n studentQuery2: select range_variable.Property");

            foreach (string s in studentQuery2)

            {

                Console.WriteLine(s);

            }

 

            // Produce a filtered sequence of objects created by

            // a method call on each Student.

            IEnumerable<ContactInfo> studentQuery3 =

                from student in app.students

                where student.ID > 111

                select student.GetContactInfo(app, student.ID);

 

            Console.WriteLine("\r\n studentQuery3: select range_variable.Method");

            foreach (ContactInfo ci in studentQuery3)

            {

                if (ci != null)

                    Console.WriteLine(ci.ToString());

            }

 

            // Produce a filtered sequence of ints from

            // the internal array inside each Student.

            IEnumerable<int> studentQuery4 =

                from student in app.students

                where student.ID > 111

                select student.Scores[0];

 

            Console.WriteLine("\r\n studentQuery4: select range_variable[index]");

            foreach (int i in studentQuery4)

            {

                Console.WriteLine("First score = {0}", i);

            }

            // Produce a filtered sequence of doubles

            // that are the result of an expression.

            IEnumerable<double> studentQuery5 =

                from student in app.students

                where student.ID > 111

                select student.Scores[0] * 1.1;

 

            Console.WriteLine("\r\n studentQuery5: select expression");

            foreach (double d in studentQuery5)

            {

                Console.WriteLine("Adjusted first score = {0}", d);

            }

 

            // Produce a filtered sequence of doubles that are

            // the result of a method call.

            IEnumerable<double> studentQuery6 =

                from student in app.students

                where student.ID > 111

                select student.Scores.Average();

 

            Console.WriteLine("\r\n studentQuery6: select expression2");

            foreach (double d in studentQuery6)

            {

                Console.WriteLine("Average = {0}", d);

            }

 

            // Produce a filtered sequence of anonymous types

            // that contain only two properties from each Student.

            var studentQuery7 =

                from student in app.students

                where student.ID > 111

                select new { student.First, student.Last };

 

            Console.WriteLine("\r\n studentQuery7: select new anonymous type");

            foreach (var item in studentQuery7)

            {

                Console.WriteLine("{0}, {1}", item.Last, item.First);

            }

 

            // Produce a filtered sequence of named objects that contain

            // a method return value and a property from each Student.

            // Use named types if you need to pass the query variable

            // across a method boundary.

            IEnumerable<ScoreInfo> studentQuery8 =

                from student in app.students

                where student.ID > 111

                select new ScoreInfo

                {

                    Average = student.Scores.Average(),

                    ID = student.ID

                };

 

            Console.WriteLine("\r\n studentQuery8: select new named type");

            foreach (ScoreInfo si in studentQuery8)

            {

                Console.WriteLine("ID = {0}, Average = {1}", si.ID, si.Average);

            }

 

            // Produce a filtered sequence of students who appear on a contact list

            // and whose average is greater than 85.

            IEnumerable<ContactInfo> studentQuery9 =

                from student in app.students

                where student.Scores.Average() > 85

                join ci in app.contactList on student.ID equals ci.ID

                select ci;

 

            Console.WriteLine("\r\n studentQuery9: select result of join clause");

            foreach (ContactInfo ci in studentQuery9)

            {

                Console.WriteLine("ID = {0}, Email = {1}", ci.ID, ci.Email);

            }

 

            #endregion

 

            #region anonymousDeligates

            //start anonymousDeligates

 

            List<Student> studentsBig =

             app.students.FindAll(delegate(Student s)

             {

                 return (s.ID > 111);

             });

 

            Console.WriteLine("Query1: select annonimus deligate range_variable");

            foreach (Student s in studentsBig)

            {

                Console.WriteLine(s.ToString());

            }

 

            List<Student> lints =

 

               app.students.FindAll(delegate(Student item)

                {

                    return ((item.Scores[0] > 60 && item.ID > 111)

                         && (item.Scores[1] > 60 && item.ID > 111)

                         && (item.Scores[2] > 60 && item.ID > 111)

                         && (item.Scores[3] > 60 && item.ID > 111)

                        );

                });

 

            app.students.ForEach(delegate(Student s)

            {

                if (s.ID > 111)

                    s.Scores[0] -= 1;

            });

 

            Student lastStu = app.students.FindLast(delegate(Student s)

            {

                return s.ID > 112;

            }

                                                    );

 

            Student firstStu = app.students.Find(delegate(Student s)

            {

                return s.ID < 120;

            }

           );

 

            int firstStudentIndex = app.students.FindIndex(delegate(Student s)//first index in list==0

            { return s.ID > 100; });

 

            int lastStudentIndex = app.students.FindLastIndex(delegate(Student s)//last index in list==3

            { return s.ID > 100; });

 

            bool weHaveID112 = app.students.Exists(delegate(Student s)

            { return s.ID == 112; });

 

            bool weHaveFirstNameSvetlana = app.students.Exists(delegate(Student s)

            { return s.First == "Svetlana"; });

 

            bool true4AllSvetlana = app.students.TrueForAll(delegate(Student s)

            { return s.First == "Svetlana"; });

 

            bool allStudentsHaveLastName =

                app.students.TrueForAll(delegate(Student s)

                { return (!string.IsNullOrEmpty(s.Last)); });

 

            app.students.ForEach(delegate(Student s)

            { s.ID += 10; });

 

            app.students.Sort(new StudendtsSorter());//sort anonymous type

 

            //end anonymousDeligates

            #endregion

 

        }

 

        #region Define some classes

        public class Student

        {

            public Student() { }

            public Student(string First, string Last, int ID, List<int> T)

            {

                this.First = First;

                this.Last = Last;

                this.ID = ID;

                this.Scores = T;

            }

 

            private string _first;

            public string First { get { return _first; } set { _first = value; } }

            private string _last;

            public string Last { get { return _last; } set { _last = value; } }

            private int _id;

            public int ID { get { return _id; } set { _id = value; } }

            public List<int> Scores;

            public ContactInfo GetContactInfo(DAL app, int id)

            {

                ContactInfo cInfo =

                    (from ci in app.contactList

                     where ci.ID == id

                     select ci)

                    .FirstOrDefault();

 

                return cInfo;

            }

 

            public override string ToString()

            {

                return First + " " + Last + ":" + ID;

            }

        }

 

        /// <summary>

        /// implement IComparer on List<Student> (for anonymousDelegate function Sort)

        /// </summary>

        class StudendtsSorter : Comparer<Student>

        {

            public override int Compare(Student x, Student y)

            {

                if (x.ID > y.ID)

                    return 1;

                else if (y.ID > x.ID)

                    return -1;

                else

                    return 0;

            }

        }

 

        public class ContactInfo

        {

            public int ID { get; set; }

            public string Email { get; set; }

            public string Phone { get; set; }

            public override string ToString() { return Email + "," + Phone; }

        }

 

        public class ScoreInfo

        {

            public double Average { get; set; }

            public int ID { get; set; }

        }

 

        #endregion

 

        #region The primary data source use in this program

        public List<Student> students = new List<Student>()

        {

             new Student {First="Svetlana", Last="Omelchenko", ID=111, Scores= new List<int>() {97, 92, 81, 60}},

             new Student {First="Claire", Last="O'Donnell", ID=112, Scores= new List<int>() {75, 84, 91, 39}},

             new Student {First="Sven", Last="Mortensen", ID=113, Scores= new List<int>() {88, 94, 65, 91}},

             new Student {First="Cesar", Last="Garcia", ID=114, Scores= new List<int>() {97, 89, 85, 82}},

        };

 

        List<ContactInfo> contactList = new List<ContactInfo>()

        {

            new ContactInfo {ID=111, Email="SvetlanO@Contoso.com", Phone="206-555-0108"},

            new ContactInfo {ID=112, Email="ClaireO@Contoso.com", Phone="206-555-0298"},

            new ContactInfo {ID=113, Email="SvenMort@Contoso.com", Phone="206-555-1130"},

            new ContactInfo {ID=114, Email="CesarGar@Contoso.com", Phone="206-555-0521"}

        };

 

        #endregion

 

    }

 

 

 

}

 

 

מאמרים נוספים...