Meo's Web site
     

http://www.meo-x.net

     Home | Information | Know-how | Sections | Documents | Services | Plus |      Blog |
 

 

 

 

 

Links / Liens:

Free C# Compilers and Interpreters

 

 

C#:

Didier Meo: Interface en C#

 

LINQ to Objects C#

a LINQ to Objects code sample

par Didier MEO, Consultant ingénieur. 15/08/2009. E-mail: didiermeo@gmail.com


using System;
using System.Collections; 
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;

    namespace TestLinq
    {
        static class Program
        {
            public class MeoAddress
            {
                public int Nr { get; set; }
                public string Name { get; set; } 
                public string Email { get; set; } 
            }

            /// 
            /// Test pour LINQ to Objects
            /// Author Didier Meo
            /// 
            static void TestLinqToObjects()
            {
                int[] intArray = { 0, 10, 20, 30, 40, 50, 60, 70, 80, 90 };
                // 1)
                string strResult = "\n iQuery1: \n";
                //
                var iQuery1 = from intObject in intArray  //from int intObject in intArray 
                              where intObject >= 30
                              select intObject;
                foreach (int intObject in iQuery1)
                {
                    strResult += Convert.ToString(intObject) + "  ;  ";
                }
                // 2)
                strResult += "\n-----------\n iQuery2: \n";
                //
                var iQuery2 = intArray.Where(intObject => intObject >= 30).Select(intObject => intObject); 
                foreach (int intObject in iQuery2)
                {
                    strResult += intObject + "  ;  ";
                }
                // 3)
                strResult += "\n-----------\n iQuery3: \n";
                //
                List<int> lstIntegers = new List<int>();
                lstIntegers.AddRange(new int[] { 0, 10, 20, 30, 40, 50, 60, 70, 80, 90 });
                var iQuery3 = lstIntegers.Where(Integer => Integer >= 30).Select(Integer => Integer);
                foreach (int Integer in iQuery3)
                {
                    strResult += Integer + "  ;  ";
                }
                //-----------------------------------------------------------------
                strResult += "\n-----------\n strQuery1: \n";
                string[] strArray = { "00", "10", "20", "30", "40", "50", "60", "70", "80", "90" };
                var strQuery1 = from strObject in strArray
                                where Convert.ToInt32(strObject) >= Convert.ToInt32("30")
                                select strObject;
                foreach (string strObject in strQuery1)
                {
                    strResult += strObject + "  ;  ";
                }
                //-----------------------------------------------------------------
                strResult += "\n-----------\n objQuery1: \n";
                ArrayList arlMeoAddresses = new ArrayList();
                arlMeoAddresses.Add(new MeoAddress { Nr = 00, Name = "000 000", Email = "000@yahoo.fr" });
                arlMeoAddresses.Add(new MeoAddress { Nr = 10, Name = "AAA aaa", Email ="aaa@yahoo.fr" });
                arlMeoAddresses.Add(new MeoAddress { Nr = 20, Name = "BBB bbb", Email = "bbb@yahoo.fr" });
                arlMeoAddresses.Add(new MeoAddress { Nr = 30, Name = "CCC ccc", Email = "ccc@yahoo.fr" });
                arlMeoAddresses.Add(new MeoAddress { Nr = 40, Name = "Meo Didier", Email = "didiermeo@yahoo.fr" });
                arlMeoAddresses.Add(new MeoAddress { Nr = 50, Name = "DDD ddd", Email = "ddd@yahoo.fr" });
                arlMeoAddresses.Add(new MeoAddress { Nr = 60, Name = "EEE eee", Email = "eee@yahoo.fr" });
                arlMeoAddresses.Add(new MeoAddress { Nr = 70, Name = "FFF fff", Email = "fff@yahoo.fr" });
                arlMeoAddresses.Add(new MeoAddress { Nr = 80, Name = "GGG ggg", Email = "ggg@yahoo.fr" });
                arlMeoAddresses.Add(new MeoAddress { Nr = 90, Name = "HHH hhh", Email = "hhh@yahoo.fr" });
                var objQuery1 = from MeoAddress objMeoAddress in arlMeoAddresses
                                where objMeoAddress.Nr >= 30 
                                //orderby objMeoAddress.Name 
                                select objMeoAddress;
                foreach (MeoAddress objMeoAddress in objQuery1)
                {
                    strResult += objMeoAddress.Nr +  " , " +  objMeoAddress.Name + " , " + objMeoAddress.Email + "  ;  \n";
                }
                //-----------------------------------------------------------------
                MessageBox.Show(strResult);
            }

            /// 
            /// Point d'entrée principal de l'application.
            /// 
            [STAThread]
            static void Main()
            {
                TestLinq.Program.TestLinqToObjects(); 
            }
        }
}

       

Description

 Language Integrated Query (LINQ) is a technology for the simple and efficient access to data, through queries directly in a .NET language (e.g. C #). LINQ to Objects enables LINQ queries directly on Collections(lists or arrays).
 Language Integrated Query (LINQ) est une technologie pour l'accès simple et effectif à des données, par des requêtes directement dans un langage .NET (p.ex. C#). LINQ to Objects permet des requêtes LINQ directement sur des listes ou champs (Collections).
 Language Integrated Query (LINQ) ist eine Technologie für den einfachen und effizienten Zugriff auf Daten, durch Abfragen direkt in einer .NET-Sprache (z.B. C#). LINQ to Objects ermöglicht LINQ-Abfragen direkt auf Auflistungen (Collections).

Didier Meo's ASP.NET web site  
 
 
 
 
 
 
 
 

 

 

Copyright © http://www.meo-x.net. ALL RIGHTS RESERVED