Querying a DataSet with DLINQ

I just started to get acquainted with the LINQ technology, specifically DLINQ.  It should probably be renamed DIMES (D*** It, Make Everything SQL).

It is definitely going to take some getting used to, but it is a cool concept.

In listing 1,  I load a Typed DataSet for the Microsoft Access FPNWIND database using two adapters: ProductsTableAdapter and CustomersTableAdapter.  All of the adapter and typed dataset code was generated for me courtesy of Data->Add New Data Source.

My first DLINQ statement gets all the cities fron the Customers table and spits them out to the console.

Here is the link statements.  First I constructed a query from a typed data row.  Then I performed the LINQ Query and finally I just spit out the results of the query to the console:

Listing 1:  Getting Data Into a Query and Using LINQ

var customerQuery = ds.Customers.ToQueryable<LINQConsoleApplication1.FPNWINDDataSet.CustomersRow>;

var query = from customer in customerQuery select customer.City;

Here is the full code for performing the list of cities to the console window:

using System;
using System.Collections.Generic;
using System.Text;
using System.Query;
using System.Xml.XLinq;
using System.Data.DLinq;
using System.Data;
using LINQConsoleApplication1.FPNWINDDataSetTableAdapters;

namespace LINQConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            FPNWINDDataSet ds = new FPNWINDDataSet();
           FPNWINDDataSetTableAdapters.CustomersTableAdapter customerAdapter = new CustomersTableAdapter();

FPNWINDDataSetTableAdapters.ProductsTableAdapter productAdapter = new ProductsTableAdapter();
            customerAdapter.Fill(ds.Customers);
            productAdapter.Fill(ds.Products);
            var customerQuery = ds.Customers.ToQueryable<LINQConsoleApplication1.FPNWINDDataSet.CustomersRow>();
            var productQuery = ds.Products.ToQueryable<LINQConsoleApplication1.FPNWINDDataSet.ProductsRow>();
            var query = from customer in customerQuery select customer.City;
            foreach (var item in query)
            {
                Console.WriteLine(item);
            }
            Console.ReadLine();
        }
    }
}

What if I only want cities beginning with M?  Then try:

var query = from customer in customerQuery where (customer.City[0] == 'M') select customer.City;

What if I want to see company name and the city?  Here you go:

var query = from customer in customerQuery where (customer.City[0] == 'M') select customer.CompanyName.PadRight(50) + customer.City;

This gives the output:
 

What if we want all products that are going to cost us more than 39 dollars? Try:

var query = from product in productQuery where (product.UnitPrice > 39) select product.ProductName.PadRight(50) + product.UnitPrice; 

Stay tuned for more fun with LINQ where we use the relationship between two tables to get our data.

-Mike

Next Recommended Reading Use of Linq to Dataset with Example