Mobile Content

Thursday, August 30, 2012

LINQ to Object Examples

LINQ: Language Integrated Query


you’ll need to persist objects to a database, query the database and load the results back into objects. The problem is that in
most cases, at least with relational databases, there is a gap between your programming language and the database. Good attempts have been made to provide object-oriented databases, which would be closer to object-oriented platforms and imperative programming languages like C# and VB.NET. However, after all these years, relational databases are still pervasive and you still have to struggle with data-access and persistence in all of your programs.The original motivation behind LINQ was to address the impedance mismatch between programming languages and databases. With LINQ, Microsoft’s intention was to provide a solution for the problem of  object-relational mapping, as well as simplify the interaction between objects and data sources. 

LINQ eventually evolved into a general-purpose language-integrated querying toolset. This toolset can be used to
access data coming from in-memory objects (LINQ to Objects), databases (LINQ to SQL), XML documents (LINQ to XML), a file-system, or from any other source.

Examples


 Response.Write("<h2>LINQ to Object</h2>");

        int[] numbers = { 5,6,7,8,3,4,9,1,2,0 };

        var no= from n in numbers
                    group n by n % 2 into g 
                    select new { Reminder=g.Key ,Number=g   };
        foreach (var g in no)
        {
            string s = string.Format("<br/>no of remainder of {0} when divided by 2:", g.Reminder);
            Response.Write(s);
            
            foreach (var n in g.Number)
            {
                Response.Write("<br/>"+n);
            }
        }
        /*
     OUTPUT
      no of remainder of 1 when divided by 2:
     5
     7
     3
     9
     1
      no of remainder of 0 when divided by 2:
     6
     8
     4
     2
     0
        */
        // for word lenth
        Response.Write("<h3>find length of word</h3>");
  string[] word = { "ABCD","EFG","HIJKL","MN","OPQRST","UVWXYZ"};

        var len = from words in word

                 // where words.Length <= 5
                 orderby words.Length ascending
                  select words;
        foreach (var leng in len)
        {
            Response.Write("<br/>" + leng + " >>Length :" + leng.Length);
        }
              
          /*
           OUTPUT
        find length of word
        MN >>Length :2
        EFG >>Length :3
        ABCD >>Length :4
        HIJKL >>Length :5
        OPQRST >>Length :6
        UVWXYZ >>Length :6
           */
                      
        //find even no

        Response.Write("<h3>find even number</h3>");

        int[] number = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 15 };

        var ev = from even in number
                 where even % 2 == 0
                 orderby even descending
                 select even;
        Response.Write("Even Number<br/>");
        foreach (var evn in ev)
        {
            Response.Write(evn+"<br/>");
        }

        /*
         OUTPUT
      find even number
      Even Number
    12
    10
    8
    6
    4
    2
       */    

No comments:

Post a Comment