LINQ to Typed DataSet

Database -> DataSet -> Typed DataSet -> ADO.NET Entity Framework -> LINQ.

Add a typed dataset by Main Menu -> Data -> Add Data Source.

     using  NorthwindDataSetTableAdapters;

    NorthwindDataSet ds 
=   new  NorthwindDataSet();
    TableAdapterManager adapters 
=   new  TableAdapterManager();

    adapters.CustomersTableAdapter 
=   new  CustomersTableAdapter();
    adapters.OrdersTableAdapter 
=   new  OrdersTableAdapter();
    adapters.Order_DetailsTableAdapter 
=   new  Order_DetailsTableAdapter();
    adapters.ProductsTableAdapter 
=   new  ProductsTableAdapter();

    adapters.CustomersTableAdapter.Fill(ds.Customers);
    adapters.OrdersTableAdapter.Fill(ds.Orders);
    adapters.Order_DetailsTableAdapter.Fill(ds.Order_Details);
    adapters.ProductsTableAdapter.Fill(ds.Products);

    
// Direct       
    Console.WriteLine( " Direct------------------------------------------- " );
    
foreach  (var row  in  ds.Customers)
        
if  (row.Country  ==   " USA " )
            Console.WriteLine(
new  { row.CustomerID, row.Country, row.ContactName });

    
// LINQ to Object
    var query  =  ds.Customers
        .Where(c 
=>  c.Country  ==   " USA " )
        .Select(c 
=>   new  { c.CustomerID, c.Country, c.ContactName });

    Console.WriteLine();
    Console.WriteLine(
" LINQ to Object------------------------------------ " );
    
foreach  (var row  in  query)
        Console.WriteLine(row);

    
// LINQ to DataSet
    DataTable customersTable  =  (DataTable)ds.Customers;
    var query2 
=
        from c 
in  customersTable.AsEnumerable()
        where c.Field
< string > ( " Country " ==   " USA "
        select c;

    Console.WriteLine();
    Console.WriteLine(
" LINQ to DataSet------------------------------------ " );
    
foreach  (var row  in  query2)
        Console.WriteLine(
new
        {
            CustomerID 
=  row.Field < string > ( " CustomerID " ),
            Country 
=  row.Field < string > ( " Country " ),
            ContactName 
=  row.Field < string > ( " ContactName " )
        });

If you create the typed DataSet with Visual Studio, your typed DataTable classes will be derived from the TypedTableBase<T> class, which implements the IEnumerable<T> interface. For this reason, it is not required to call AsEnumerable to get a wrapper. 
    var query2  =
        
// from c in customersTable.AsEnumerable()
        from c  in  ds.Customers
        where c.Field
< string > ( " Country " ==   " USA "
        select c;

 

Reference


MSDN: LINQ to DataSet
http://msdn.microsoft.com/en-us/library/bb386977.aspx

 

你可能感兴趣的:(LINQ)