Makeup Querying Collections

filtering a collection [LINQ][Evolution in action] C#1.1=>2.0=>3.0 [LINQ and query expressions]

       static void Main()
       {
           List<Product> products = Product.GetSampleProducts();
           var filtered =
from Product p in products
                                where p.Price > 10
                                select p;
           foreach (Product product in filtered)
           {
               Console.WriteLine(product);
           }
       }

LINQ is what C#3.5 is all about at its heart. In particular, it contains query expressions that allow a declarative style for creating queries on various data sources.


Actually, LINQ is that while query expressions are not particularly suitable for simple task,they're very, very good for more complicated situations that would be hard to read if written out in the equivalent method calls.


Now let's look at a code to join the sample products with the sample suppliers (obviously based on the supplier ID), apply the same price filter as before to the products, sort by supplier name and then product name, and print out the name of  both supplier and product for each match.


*So , in earlier versions of C# it would have been a nightmare to implement. In LINQ, it's almost trivial!

Joing ,filtering , ordering, and projecting[Evolution in action] C#1.1=>2.0=>3.0 [LINQ and query expressions]
[Evolution in action] C#1.1=>2.0=>3.0 [LINQ and query expressions]      static void Main()

It looks remarkably like SQL, This is the reaction of many people on first hearing.

So, we maybe getting data from any number of sources:XML[Evolution in action] C#1.1=>2.0=>3.0 [LINQ and query expressions]
[Evolution in action] C#1.1=>2.0=>3.0 [LINQ and query expressions]<?xml version="1.0"?>
[Evolution in action] C#1.1=>2.0=>3.0 [LINQ and query expressions]
<Data>
[Evolution in action] C#1.1=>2.0=>3.0 [LINQ and query expressions]  
<Products>
[Evolution in action] C#1.1=>2.0=>3.0 [LINQ and query expressions]    
<Product Name="Company" Price="9.99" SupplierID="1" />
[Evolution in action] C#1.1=>2.0=>3.0 [LINQ and query expressions]    
<Product Name="Assassins" Price="14.99" SupplierID="2" />
[Evolution in action] C#1.1=>2.0=>3.0 [LINQ and query expressions]    
<Product Name="Frogs" Price="13.99" SupplierID="1" />
[Evolution in action] C#1.1=>2.0=>3.0 [LINQ and query expressions]    
<Product Name="Sweeney Todd" Price="10.99" SupplierID="3" />
[Evolution in action] C#1.1=>2.0=>3.0 [LINQ and query expressions]  
</Products> 
[Evolution in action] C#1.1=>2.0=>3.0 [LINQ and query expressions]
[Evolution in action] C#1.1=>2.0=>3.0 [LINQ and query expressions]  
<Suppliers>

[Evolution in action] C#1.1=>2.0=>3.0 [LINQ and query expressions]    
<Supplier Name="Solely Sondheim" SupplierID="1" />
[Evolution in action] C#1.1=>2.0=>3.0 [LINQ and query expressions]    
<Supplier Name="CD-by-CD-by-Sondheim" SupplierID="2" />
[Evolution in action] C#1.1=>2.0=>3.0 [LINQ and query expressions]    
<Supplier Name="Barbershop CDs" SupplierID="3" />
[Evolution in action] C#1.1=>2.0=>3.0 [LINQ and query expressions]  
</Suppliers>
[Evolution in action] C#1.1=>2.0=>3.0 [LINQ and query expressions]
</Data> 
[Evolution in action] C#1.1=>2.0=>3.0 [LINQ and query expressions]

Well,the xml file is simple enough, but what's the best way of extracting the data from it?

Query it? Join on it?

OK, continue


Shows how much work we have to do in
LINQ to XML[Evolution in action] C#1.1=>2.0=>3.0 [LINQ and query expressions]

[Evolution in action] C#1.1=>2.0=>3.0 [LINQ and query expressions]  static void Main()

Keyword: XDocument,Descendants,Attribute

Well, it's not quite as straightforward, because we need to tell the system how it should understand the data(in terms of what attributes should be used as what types)[Evolution in action] C#1.1=>2.0=>3.0 [LINQ and query expressions]


Ok, Let us put the data where it's much more likely to be- in a database.
Shows how much work we have to do in LINQ to SQL[Evolution in action] C#1.1=>2.0=>3.0 [LINQ and query expressions]
Step one: Create  dbml  
[Evolution in action] C#1.1=>2.0=>3.0 [LINQ and query expressions]
Step two: Haulage DataTable
[Evolution in action] C#1.1=>2.0=>3.0 [LINQ and query expressions]
Step three: Looking

[Evolution in action] C#1.1=>2.0=>3.0 [LINQ and query expressions] static void Main()

This code issues a database request, which is basically the query transalted into SQL.

Even though we've expressed the query in C# code, it's been executed as SQL.

We'll see later that the way this query joins isn’t how we’d normally use LINQ to SQL— there's a more relation-oriented way of approaching it when the schema and the entities know about the relationship between suppliers and products.

The result is the same, however, and it shows just how similar LINQ to Objects (the in-memory LINQ operating on collections) and LINQ to SQL can be.


It's important to understand that LINQ is flexible, too: you can write your own
query translators. It’s not easy, but it can be well worth it.

For instance: => LINQ to Amazon

[Evolution in action] C#1.1=>2.0=>3.0 [LINQ and query expressions]

Wow LINQ to Everything [Evolution in action] C#1.1=>2.0=>3.0 [LINQ and query expressions]

So much for this![Evolution in action] C#1.1=>2.0=>3.0 [LINQ and query expressions]

相关文章: