Remote and Local execution of Query in LINQ


Remote Execution of Query

  1. This query executes on the server.
  2. Remote execution of query is default in LINQ.
  3. In Remote execution advantage of Databases index can be taken.
  4. Remote execution allows us to take the advantage of Database server engine.
  5. Remote execution allows us to only select particular rows from the table. This is very useful when we do have large amount of data in the server.

Remote execution is being default and same as majority of the query we write in LINQ.

Program.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Linq ; 
 
namespace ConsoleApplication5
  {
    class Program
        {
            static void Main(string[] args)
                {
                    DataClasses1DataContext context = new DataClasses1DataContext();                   
                    var result = from r in context.Persons orderby r.FirstName select r;
                    foreach (var r in result)
                    {
                        Console.WriteLine(r.FirstName + " " + r.LastName);
                    }
                    Console.ReadKey(true);
                }
        }


Output

1.gif


Local Execution of Query

  1. In local execution of query they get executed in local cache.
  2. The biggest advantage is who data loaded locally can be serialized.
  3. For each time to get Data there is no need to go back to server.
  4. Using Load of local execution related entities can be fetched together.
  5. Load() method is used to execute query locally.

    2.gif

In above query Person table and StudentGrades table are related to each other.

Program.cs

using System;
using System.Linq;
using System.Data.Linq;
namespace ConsoleApplication5
  {
    class Program
        {
            static void Main(string[] args)
                {
                   DataClasses1DataContext context = new DataClasses1DataContext();                  

                    Person  p = context.Persons .Single(res => res.PersonID   == 9);                 
                    p.StudentGrades.Load();
                    foreach (var r in p.StudentGrades)
                    {
                        Console.WriteLine(r.Grade);
                    }
                    Console.ReadKey(true);
                    
                }
        }
}


Output

3.gif

Up Next
    Ebook Download
    View all
    Learn
    View all