Server Mode with LINQ to SQL

  • 3 minutes to read

The PivotGridControl automatically operates in server mode if it is bound to the LINQ to SQL data source. This example shows how to use LINQ to SQL in the Pivot Grid application.

Creating Data Classes

  1. Add LINQ to SQL Classes to the project.

    Server Mode LINQ  Add LINQ to SQL classes

  2. Data classes can then be created and edited in an Object Relational Designer (O/R Designer). An O/R Designer provides a visual design surface for creating LINQ to SQL entity classes and relationships based on objects in a database. To learn more, see the Microsoft LINQ to SQL article.

    You can create and map entity classes to tables and views by dragging database tables and views from Server Explorer onto the O/R Designer.

    Server Mode LINQ OR Designer

    Save your changes, close the O/R Designer, and rebuild the solution.

Binding a Pivot Grid Control to the LinqServerModeSource component

  1. Drag the LinqServerModeSource component and drop it onto the Form.

    Server Mode Add LINQ Component

    NOTE

    Alternatively, you can create the LinqServerModeSource in code at runtime.

  2. Specify the type of objects retrieved from a data source with the LinqServerModeSource.ElementType and LinqServerModeSource.KeyExpression properties:

    linqServerModeSource1.ElementType = typeof(Invoice);
    linqServerModeSource1.KeyExpression = "OrderID";
    
  3. Specify the queryable source using the LinqServerModeSource.QueryableSource property:

    linqServerModeSource1.QueryableSource = dc.Invoices;
    
  4. Bind the Pivot Grid control to the LinqServerModeSource component:

    pivotGridControl1.DataSource = linqServerModeSource1;
    

    The resulting code looks as follows:

    using DevExpress.Data.Linq;
    using DevExpress.XtraEditors;
    using System;
    
    namespace LinqToSqlServerModeExample
    {
        public partial class Form1 : DevExpress.XtraEditors.XtraForm
        {
            LinqServerModeSource linqServerModeSource1;
            bool serverMode = false;
            public Form1()
            {
                InitializeComponent();
                // Create LINQ Server Mode data source.
                linqServerModeSource1 = new LinqServerModeSource
                {
                    ElementType = typeof(Invoice),
                    KeyExpression = "OrderID"
                };
                // Create the data context and enable query logging.
                NWindDataContext dc = new NWindDataContext { Log = Console.Out };
                //
                // Specify the queryable source that provides data items.
                linqServerModeSource1.QueryableSource = dc.Invoices;
            }
    
            private void Form1_Load(object sender, EventArgs e)
            {
                pivotGridControl1.DataSource = linqServerModeSource1;
            }
        }
    }
    
  5. Run the project. The PivotGridControl works in server mode because it is bound to the LINQ-to-SQL data source. You can see the generated SQL statements in the Visual Studio Output window.

NOTE

The complete sample project Pivot Grid LINQ to SQL Server Mode Example is available in the DevExpress Examples repository.

See Also