Dennis' Blog
Avonturen in .NET
posts - 37, comments - 268, trackbacks - 0, articles - 0
My Links
Home
Contact
Login
Archives
March, 2006 (1)
February, 2006 (6)
January, 2006 (3)
November, 2005 (1)
October, 2005 (7)
September, 2005 (19)
Post Categories
C#
From the trenches
PDC
Security
Visual Studio 2005 Team System
Image Galleries
dotNED
Bloggers
Hassan Fadili
Michiel van Otegem
Peter van Ooijen
Misc
Detrio
dotNED, De Nederlandse .net gebruikersgroep
Een eerste blik op LINQ
Posted on Thursday, November 10, 2005 10:10 PM
Het is alweer enige tijd geleden dat ik iets over C# 3.0 geschreven heb, maar dat ga ik nu (min of meer) goedmaken. Ik vervolg deze serie nu met een aantal artikelen over een van de toepassingen van de nieuwe mogelijkheden: Linq.
Linq staat voor Language Integrated Query. Zoals de naam al aangeeft geeft ons dat de mogelijkheid om in de taal (in dit geval C# maar VB.Net kan er ook mee overweg) queries te definieren. Het verhaal achter het ontstaan van Linq zal ik hier even kort schetsen, zodat je een beeld hebt wat de geschiedenis is.
Neem eens een stuk code dat je recentelijk geschreven hebt. Kijk er eens goed naar en ga dan kijken hoe vaak je in die code "zoekt" naar zaken. Denk bijvoorbeeld aan het doorlopen van een ArrayList instance met daarin Customer objecten. Regelmatig schrijf je code die in die lijst op zoek gaat naar alle klanten die nog een betaling open hebben staan. Of je wilt een overzicht van alle klanten in je lijst die aangegeven hebben dat ze informatie per email willen ontvangen. Of je wilt simpelweg in die lijst op zoek gaan naar die ene klant met klantnummer 'xyz123' zodat je de gegevens daarvan op het scherm kunt tonen. Als je goed kijkt, zul je zien dat het zoeken naar gegevens in lijsten (in wat voor vorm dan ook) best vaak voorkomt.
Echter, een goede methode om te zoeken is er niet. Neem de volgende code eens:
// Initializeer de boel
Customer displayCustomer = null;
bool bFound = false;
int counter = 0;
// Zoek de juiste klant
while( (!bFound) && (counter < customerList.Count) ) {
displayCustomer = (Customer)customerList[counter];
if( displayCustomer.CustomerID == "xyz123" ) {
bFound = true;
}
counter++;
}
if( bFound ) {
// Laat zien
} else {
// Geef foutmelding
}
// Enzovoorts...
Ik weet het, deze code kan beter. Je kunt natuurlijk een specifieke CustomerList type definieren, met daarin de juiste zoekcode. Maar die zal er niet echt anders uitzien dan bovenstaande code.
Zoeken is behoorlijk belangrijk de meeste, serieuze applicaties. Er zijn vele voorbeelden te verzinnen: zoeken naar de inlognaam van de gebruiker om zijn of haar wachtwoord te controleren, zoeken naar de bestanden in de plugin-directory, zoeken naar de geinstalleerde printers op het systeem, zoeken naar.. affijn, je snapt het wel.
Zoeken gebeurdt uiteraard ook in databases. Da's mooi, zul je denken, want daar hoef ik niet zo veel voor te doen. Alles wat ik moet doen is de juiste SQL code schrijven en de database regelt het zelf wel. Maar heb je je wel eens gerealiseerd dat dat eigenlijk een hele vreemde constructie oplevert? Je schrijft hele mooie, goed gedocumenteerde, door de compiler op alle details gecontroleerde C# of VB.Net code, vervolgens plak je daar een string (!!!) in met daarin een stuk code in een hele andere taal, namelijk SQL. Denk daar eens over na: in onze code staat opeens een niet te controleren, niet te compileren en dus niet te verifieren string met een andere programmeertaal daarin. Vreemd, vind je niet?
Als je nu veel met XML werkt (en wie doet dat tegenwoordig niet?) dan krijg je te maken met weer een hele andere manier van zoeken in data. Nu kun je met XQuery of XPath aan de gang gaan, of je itereert zelf over de elementen in je XML document heen en goet alles zelf.
Zoeken, zoeken en zoeken. We doen het nogal eens in onze applicatie, op heel veel verschillende en elkaar min of meer tegensprekende methodes. Om daar nu wat aan te doen, is het Project Linq geboren. Het idee hierachter is: laten we alle mogelijke manieren van zoeken (querien in slecht nederlands) nu eens onder de loep nemen en daar een uniforme, strongly typed, verifieerbare en compileerbare methode voor verzinnen. En dat hebben ze gedaan.
Al gauw bleek dat dat niet zo eenvoudig was en om dat op een goede manier te doen moest de taal C# en VB.Net aangepast worden. Welke aanpassingen dat zijn, heb je in mijn vorige postings kunnen lezen (ja ja, er zat een lijn in het verhaal!). Met die uitbreidingen is Linq mogelijk geworden. Ik zal nog even op een rijtje zetten welke nieuwe mogelijkheden er komen:
Type inferrence (var q = ....)
Extension methods
Initializers
Anonymous types
Lambda expressions
Met dit alles kunnen we bovenstaande code anders gaan schrijven, op een manier die ons behoorlijk bekend voor zal komen:
var displayCustomer = from customer in customerList
where customer.CustomerID == "xyz123"
select customer;
if (displayCustomer != null)
{
// Laat zien
}
else
{
// Geef foutmelding
}
En dit doet dus precies hetzelfde...
Laat je niet verwarren door de rare plaatsing van 'select', dit leg ik nog wel uit. Maar afgezien daarvan is dit 'gewoon' SQL in C# syntax. De enige aanpassing die ik gedaan heb is dat customerList nu geen ArrayList meer is maar een List<Customer>. Dat maakt echter voor mijn verhaal niets uit.
Laten we eens kijken hoe dit nou kan.
Je moet weten dat de namespace System.Query (waar Linq in is gedefnieerd) voornamelijk bestaat uit een aantal extension methods op het type IEnumerable<T>. Met andere woorden: alles wat de interface IEnumerable<T> implementeert heeft een aantal extra methods gekregen. Een van die methods is bijvoorbeeld Where(); Die zit er als volgt uit:
IEnumerable<T> IEnumerable.Where<T>((T) => bool : predicate)
Met andere woorden: de method Where heeft als resultaat een IEnumerable<T> en als argument de lambda expression (T) => bool: predicate, oftewel er wordt een anonymous method gemaakt die een bool terug geeft. Dus als we dat even toepassen op onze customerList gaat dat er als volgt uitzien:
var resultList = customerList.Where( (Customer c) => c.CustomerID == "xyz123" );
Vertaling: neem onze List<Customer> customerList (welke IEnumerable<Customer> implementeert). Voer daar de 'Where' method op uit. In die Where method geven we mee de lambda expressie (nogmaals, lees mijn vorige postings over dit onderwerp om te begrijpen wat dat precies is) (Customer c) => c.CustomerID == "xyz123" . Er wordt nu dus een anonymous method gedefnieerd die voor iedere Customer instance in de lijst kijkt naar de CustomerID en die vergelijkt met "xyz123". Daar komt een bool uit (true of false) en als hij true is, dan wordt die Customer instance aan de nieuwe resultList toegevoegd. Overigens is resultList ook een class die IEnumerable<T> implementeert....
Kijk hier nog eens goed naar, laat het even bezinken. Ik wacht wel even.
Goed. Het resultaat is dus een IEnumerable<T>. Daarop kunnen we dan weer andere extension methods toepassen, welke bijna allemaal een IEnumerable<T> terug geven. We kunnen dus een hele rij van dit soort methods aanroepen: Lijst.Where().Where().GroupBy().Select(); Anders weergegeven:
Lijst.Where().
Where().
GroupBy().
Select();
Nou hebben we net gezien in mijn voorbeeld dat we helemaal geen IEnumerable<T>.Where() method gebruiken. Nee, we roepen gewoon from type x in lijst where x.iets == "bla" select x.ID; of zo iets dergelijks. De reden dat dat werkt is door het gebruik van Expression Trees. Dat is wellicht iets voor andere posting, maar neem nu maar even van me aan dat dat er voor zorgt dat onze mooie, SQL-achtige syntax verandert in die reeks van method calls en dat alle code achter bijvoorbeeld de where vertaalt wordt in een lambda expressie. Moeilijk is dat niet, het is een kwestie van haakjes en dergelijke toevoegen.
Dat betekent dat onze eerste voorbeeld code er eigenlijk zo uit gaat zien:
var displayCustomer = customerList
.Where((Customer customer) => customer.CustomerID == "xyz123")
.Select(customer => customer);
En deze code moet nu te begrijpen zijn.
Je ziet het: als je er even naar kijkt, is het op zich vrij logisch. Maar je moet er even aan wennen. Persoonlijk vind ik mijn eerste Linq voorbeeld qua code duidelijker en overzichtelijker dan mijn eerst code-voorbeeld in deze posting.
Volgende keer ga ik kijken naar DLinq, oftewel Linq'en op een database. Het zal je niet verbazen dat dat vrijwel hetzelfde werkt als wat ik je nu heb laten zien.
Tot dan!
PS Mocht je opmerkingen, vragen of iets anders te melden hebben, aarzel dan niet om te reageren, hetzij via het commentaarvakje, hetzij via de mail. Op die manier weet ik of er uberhaupt mensen geinteresseerd zijn in wat ik te melden heb over dit onderwerp.
Feedback
# re: Een eerste blik op LINQ
5/26/2007 11:10 PM by sohbet
http://www.kitlen.com
http://kitlen.com/ruya-tabirleri.htm
http://kitlen.com/video.htm
http://kitlen.com/ask_sevgi.htm
http://kitlen.com/flash_oyunlar.htm
http://kitlen.com/sohbet.php
http://sohbet.kitlen.com
http://www.kodes.com
http://irc.kitlen.com
http://kitlen.com/yonja.php
Thank you
# re: Een eerste blik op LINQ
11/7/2007 9:58 PM by telefonsex
telefonsex
# re: Een eerste blik op LINQ
11/7/2007 9:59 PM by telefonsex
telefonsex
# re: Een eerste blik op LINQ
11/7/2007 10:00 PM by sex
sex
# Sohbet
11/24/2007 1:23 PM by Sohbet
http://www.sohbetiy.com">
http://www.sohbetiy.com
# Chat
11/24/2007 1:24 PM by Chat
http://www.sohbetiy.com">
http://www.sohbetiy.com
# Kurtalan
11/30/2007 1:51 PM by Kurtalan
Thanks your..
# SiirtChat
11/30/2007 1:53 PM by SiirtSohbet
Thank your, Good Job!
# re: Een eerste blik op LINQ
1/26/2008 5:33 PM by sohbet
THANGYOU
http://www.trsohbeti.net
http://www.sohbet99.net
http://www.muhabbethane.com
http://www.nesohbet.com
http://www.mirc10.com
http://www.sozsohbet.com
http://oyular.blogspot.com
http://mirctr.blogspot.com
# re: Een eerste blik op LINQ
1/29/2008 11:26 AM by Sohbet Video Youtube
http://www.sohbetseli.net
http://sohbetseli.net/ruya-tabirleri.htm
http://sohbetseli.net/guzelsozler.htm
http://sohbetseli.net/ask_sevgi.htm
http://sohbetseli.net/flash_oyunlar.htm
http://sohbetseli.net/sohbet.php
http://arsiv.sohbetseli.net
http://oyun.sohbetseli.net
http://program.sohbetseli.net
http://sex-hikayeleri.sohbetseli.net
http://sexhikayeleri.sohbetseli.net
http://sarkisozleri.sohbetseli.net
http://fikra.sohbetseli.net
http://ruyatabirleri.sohbetseli.net
http://www.sohbet4.com
http://www.youtube-tr.net
http://www.sohbet-turk.com
http://www.asksohbet.gen.tr
# sohbet
1/29/2008 2:33 PM by sohbet
thanx
# sohbet
1/31/2008 6:52 PM by Sohbet
thanx shadow
# re: Een eerste blik op LINQ
2/7/2008 7:19 PM by mirc
Thanks Best Regards
# re: Een eerste blik op LINQ
2/7/2008 7:25 PM by mirc
Thanks Best Regards
http://www.turkiyemirc.org/">
http://www.turkiyemirc.org/
http://www.turkiyemirc.org/">
http://www.turkiyemirc.org/forum
http://www.turkiyemirc.org/">
http://www.turkiyemirc.org/forum/
http://www.turkiyemirc.org/">
http://www.turkiyemirc.org/chat/
http://www.turkiyemirc.org/">
http://www.turkiyemirc.org/muhabbet/
http://www.karar.org">
http://www.karar.org
http://www.karar.org">
http://www.karar.org/forum/
http://www.karar.org">
http://www.karar.org/sohbet/
http://www.turkada.net/">
http://www.turkada.net/
http://www.turkada.net/">
http://www.turkada.net/forum/
http://www.sohbettr.gen.tr">
http://www.sohbettr.gen.tr
http://www.sohbettr.gen.tr">
http://www.sohbettr.gen.tr/toplist/
http://www.mirc-turk.gen.tr/">
http://www.mirc-turk.gen.tr/
# re: Een eerste blik op LINQ
2/7/2008 7:41 PM by mirc
Thanks Best Regards
http://www.turkiyemirc.org/">
http://www.turkiyemirc.org/
http://www.turkiyemirc.org/">
http://www.turkiyemirc.org/forum
http://www.turkiyemirc.org/">
http://www.turkiyemirc.org/forum/
http://www.turkiyemirc.org/">
http://www.turkiyemirc.org/chat/
http://www.turkiyemirc.org/">
http://www.turkiyemirc.org/muhabbet/
http://www.karar.org">
http://www.karar.org
http://www.karar.org">
http://www.karar.org/forum/
http://www.karar.org">
http://www.karar.org/sohbet/
http://www.turkada.net/">
http://www.turkada.net/
http://www.turkada.net/">
http://www.turkada.net/forum/
http://www.sohbettr.gen.tr">
http://www.sohbettr.gen.tr
http://www.sohbettr.gen.tr">
http://www.sohbettr.gen.tr/toplist/
http://www.mirc-turk.gen.tr/">
http://www.mirc-turk.gen.tr/
# re: Een eerste blik op LINQ
2/23/2008 1:30 PM by Remy
Ik weet niet wat voor een rare reacties er allemaal op dit stukje zeer handige informatie worden gegeven (dat ondertussen al een paar jaartjes oud is), maar ik heb hier heel erg veel aan! Bedankt!
Mvg Remy
# görüntülü sohbet
2/26/2008 5:06 PM by görüntülü sohbet
Death
# re: Een eerste blik op LINQ
6/3/2008 1:17 PM by porno izle
http://www.sexizlee.com
http://www.sexizleyin.com
http://www.forumsayfam.com
http://www.multiadult.com
http://www.pornsayfam.com
http://www.sicakblog.com
http://www.atesliblog.com
http://www.pornvideolar.com
http://www.hotizle.com
http://www.tubesexy.net
http://www.ateslifilmler.com
http://www.sexisayfa.com
http://www.sicakoyun.com
http://www.ateslitube.com
http://www.atesliporno.com
http://www.sexysayfa.com
http://www.atesliizle.com
http://www.sicakfilmler.com
http://www.ateslierotik.com
http://www.erotikciler.com
http://www.yetiskinvideo.com
http://www.hikayesayfam.com
http://www.89movie1.com
http://www.8adult.com
http://www.tubeporn1.com
http://www.sexywomanz.com
http://www.bikinisexy.net
http://www.freepornsexy.net
http://www.fierytube.com
http://www.sicaksayfa.com
http://www.adultsayfa.com
http://www.ateslifilm.com
http://www.yetiskinvideo.net
http://www.yetiskinvideo.org
http://www.yetiskinvideolar.com
http://www.yetiskinvideolar.net
http://www.oyunf.com
http://www.theoyun.net
http://www.oyunh.com
http://www.videosayfam.com
http://www.videosayfam.net
http://www.forumsayfam.net
http://www.mixarticle.com
http://www.indirmedenizle.net
http://www.videosayfan.com
http://www.vidfull.com
http://www.videolar1.com
http://www.videop.net
http://www.videolar1.net
http://www.videolarz.com
http://www.videolarz.net
http://www.videolars.com
http://www.utubem.com
http://www.izlenix.com
http://www.izlemeyeri.com
http://www.izlematic.com
http://forumsayfam.blogspot.com
http://yenioyunlaroyun.blogspot.com
http://yemekoyunlarioyna.blogspot.com
http://kizoyunlarioyna.blogspot.com
http://makyajyapoyunu.blogspot.com
http://oyunlaroyunu.blogspot.com
http://atesli-film.blogspot.com
http://fovermix.blogspot.com
http://newscelebrityy.blogspot.com
# Laptop
12/17/2008 7:56 PM by
http://laptop-bilgisayar.blogspot.com/">
http://laptop-bilgisayar.blogspot.com/">
http://laptop-bilgisayar.blogspot.com/">
http://laptop-bilgisayar.blogspot.com/
http://okeyindirr.blogspot.com/">
http://okeyindirr.blogspot.com/
http://bedavaklipindir.blogspot.com/">
http://bedavaklipindir.blogspot.com/
http://zayiflatma.blogspot.com/">
http://zayiflatma.blogspot.com/
http://saglikliguzellik.blogspot.com/">
http://saglikliguzellik.blogspot.com/
http://quality-video.blogspot.com/">
http://quality-video.blogspot.com/
http://fullmp3dinle.blogspot.com/">
http://fullmp3dinle.blogspot.com/
http://kanserin-tedavileri.blogspot.com/">
http://kanserin-tedavileri.blogspot.com/
http://mpler.blogspot.com/">
http://mpler.blogspot.com/
http://bedava--lig-tv.blogspot.com/">
http://bedava--lig-tv.blogspot.com/
http://laptop-bilgisayar.blogspot.com/">
http://laptop-bilgisayar.blogspot.com/">
http://laptop-bilgisayar.blogspot.com/">
http://laptop-bilgisayar.blogspot.com/
http://www.siirbahcesi.net/">
http://www.siirbahcesi.net/
http://www.yenimp3ara.org/">
http://www.yenimp3ara.org/
http://indirklipizle.blogcu.com/">
http://indirklipizle.blogcu.com/
# adult atesli videolar
1/4/2009 7:49 PM by adult atesli vieolar
http://okeyindirr.blogspot.com/">
http://okeyindirr.blogspot.com/
http://bedavaklipindir.blogspot.com/">
http://bedavaklipindir.blogspot.com/
http://zayiflatma.blogspot.com/">
http://zayiflatma.blogspot.com/
http://saglikliguzellik.blogspot.com/">
http://saglikliguzellik.blogspot.com/
http://quality-video.blogspot.com/">
http://quality-video.blogspot.com/
http://fullmp3dinle.blogspot.com/">
http://fullmp3dinle.blogspot.com/
http://kanserin-tedavileri.blogspot.com/">
http://kanserin-tedavileri.blogspot.com/
http://mpler.blogspot.com/">
http://mpler.blogspot.com/
http://bedava--lig-tv.blogspot.com/">
http://bedava--lig-tv.blogspot.com/
http://laptop-bilgisayar.blogspot.com/">
http://laptop-bilgisayar.blogspot.com/">
http://laptop-bilgisayar.blogspot.com/">
http://laptop-bilgisayar.blogspot.com/
http://www.siirbahcesi.net/">
http://www.siirbahcesi.net/
http://www.yenimp3ara.org/">
http://www.yenimp3ara.org/
http://indirklipizle.blogcu.com/">
http://indirklipizle.blogcu.com/
# re: Een eerste blik op LINQ
2/13/2009 4:51 PM by sohbet
http://www.sohbetmetro.com
# re: Een eerste blik op LINQ
7/8/2009 10:57 AM by Introducing C# 3 – Part 4 This is the forth of a s
Introducing C# 3 – Part 4
This is the forth of a series of articles exploring new features in C# 3. The first three have covered:
Implicitly typed variables and arrays
Extension methods and lambda expressions
Object and collection initializers and anonymous types
To understand this part you should have read the previous three parts of the series, since we will be drawing on features described in all of them.
Introducing Linq
Linq is short for Language Integrated Query. If you are used to using SQL to query databases, you are going to have something of a head start with Linq, since they have many ideas in common. Before we dig into Linq itself, let's step back and look at what makes SQL different from C#.
Imagine we have a list of orders. For this example, we will imagine they are stored in memory, but they could be in a file on disk too. We want to get a list of the costs of all orders that were placed by the customer identified by the number 84. If we set about implementing this in C# before version 3 and a range of other popular languages, we would probably write something like (assuming C# syntax for familiarity):
List<double> Found = new List<double>();
foreach (Order o in Orders)
if (o.CustomerID == 84)
Found.Add(o.Cost);
Here we are describing how to achieve the result we want by breaking the task into a series of instructions. This approach, which is very familiar to us, is called imperative programming. It relies on us to pick a good algorithm and not make any mistakes in the implementation of it; for more complex tasks, the algorithm is more complex and our chances of implementing it correctly decrease.
If we had the orders stored in a table in a database and we used SQL to query it, we would write something like:
SELECT Cost FROM Orders WHERE CustomerID = 84
Here we have not specified an algorithm, or how to get the data. We have just declared what we want and left the computer to work out how to do it. This is known as declarative or logic programming.
Linq brings declarative programming features into imperative languages. It is not language specific, and has been implemented in the Orcas version of VB.Net amongst other languages. In this series we are focusing on C# 3.0, but the principles will carry over to other languages.
Understanding A Simple Linq Query
Let's jump straight into a code example. First, we'll create an Order class, then make a few instances of it in a List as our test data. With that done, we'll use Linq to get the costs of all orders for customer 84.
class Order
{
private int _OrderID;
private int _CustomerID;
private double _Cost;
public int OrderID
{
get { return _OrderID; }
set { _OrderID = value; }
}
public int CustomerID
{
get { return _CustomerID; }
set { _CustomerID = value; }
}
public double Cost
{
get { return _Cost; }
set { _Cost = value; }
}
}
class Program
{
static void Main(string[] args)
{
// Set up some test orders.
var Orders = new List<Order> {
new Order {
OrderID = 1,
CustomerID = 84,
Cost = 159.12
},
new Order {
OrderID = 2,
CustomerID = 7,
Cost = 18.50
},
new Order {
OrderID = 3,
CustomerID = 84,
Cost = 2.89
}
};
// Linq query.
var Found = from o in Orders
where o.CustomerID == 84
select o.Cost;
// Display results.
foreach (var Result in Found)
Console.WriteLine("Cost: " + Result.ToString());
}
}
The output of running this program is:
Cost: 159.12
Cost: 2.89
Let's walk through the Main method. First, we use collection and object initializers to create a list of Order objects that we can run our query over. Next comes the query - the new bit. We declare the variable Found and request that its type be inferred for us by using the "var" keyword.
We then run across a new C# 3.0 keyword: "from".
from o in Orders
This is the keyword that always starts a query. You can read it a little bit like a "foreach": it takes a collection of some kind after the "in" keyword and makes what is to the left of the "in" keyword refer to a single element of the collection. Unlike "foreach", we do not have to write a type.
Following this is another new keyword: "where".
where o.CustomerID == 84
This introduces a filter, allowing us to pick only some of the objects from the Orders collection. The "from" made the identifier "o" refer to a single item from the collection, and we write the condition in terms of this. If you type this query into the IDE yourself, you will notice that it has worked out that "o" is an Order and intellisense works as expected.
The final new keyword is "select".
select o.Cost
This comes at the end of the query and is a little like a "return" statement: it states what we want to appear in the collection holding the results of the query. As well as primitive types (such as int), you can instantiate any object you like here. In this case, we will end up with Found being a List<int>, though.
You may be thinking at this point, "hey, this looks like SQL but kind of backwards and twisted about a bit". That is a pretty good summary. I suspect many who have written a lot of SQL will find the "select comes last" a little grating at first; the other important thing to remember is that all of the conditions are to be expressed in C# syntax, not SQL syntax. That means "==" for equality testing, rather than "=" in SQL. Thankfully, in most cases that mistake will lead to a compile time error anyway.
A Few More Simple Queries
We may wish our query to return not only the Cost, but also the OrderID for each result that it finds. To do this we take advantage of anonymous types.
var Found = from o in Orders
where o.CustomerID == 84
select new { OrderID = o.OrderID, Cost = o.Cost };
Here we have defined an anonymous type that holds an OrderID and a Cost. This is where we start to see the power and flexibility that they offer; without them we would need to write custom classes for every possible set of results we wanted. Remembering the projection syntax, we can shorten this to:
var Found = from o in Orders
where o.CustomerID == 84
select new { o.OrderID, o.Cost };
And obtain the same result. Note that you can perform whatever computation you wish inside the anonymous type initializer. For example, we may wish to return the Cost of the order with an additional sales tax of 10% added on to it.
var Found = from o in Orders
where o.CustomerID == 84
select new {
o.OrderID,
o.Cost,
CostWithTax = o.Cost * 1.1
};
Conditions can be more complex too, and are built up in the usual C# way, just as you would do in an "if" statement. Here we apply an extra condition that we only want to see orders valued over a hundred pounds.
var Found = from o in Orders
where o.CustomerID == 84 && o.Cost > 100
select new {
o.OrderID,
o.Cost,
CostWithTax = o.Cost * 1.1
};
Ordering
It is possible to sort the results based upon a field or the result of a computation involving one or more fields. This is achieved by using the new "orderby" keyword.
var Found = from o in Orders
where o.CustomerID == 84
orderby o.Cost ascending
select new { o.OrderID, o.Cost };
After the "orderby" keyword, we write the expression that the objects will be sorted on. In this case, it is a single field. Notice this is different from SQL, where there are two words: "ORDER BY". I have added the keyword "ascending" at the end, though this is actually the default. The result is that we now get the orders in order of increasing cost, cheapest to most expensive. To get most expensive first, we would have used the "descending" keyword.
While I said earlier that the ordering condition is based on fields in the objects involved in the query, it actually doesn't have to be. Here's a way to get the results in a random order.
Random R = new Random();
var Found = from o in Orders
where o.CustomerID == 84
orderby R.Next()
select new { OrderID = o.OrderID, Cost = o.Cost };
Joins
So far we have just had one type of objects to run our query over. However, real life is usually more complex than this. For this example, let's introduce another class named Customer.
class Customer
{
private int _CustomerID;
private string _Name;
private string _Email;
public int CustomerID
{
get { return _CustomerID; }
set { _CustomerID = value; }
}
public string Name
{
get { return _Name; }
set { _Name = value; }
}
public string Email
{
get { return _Email; }
set { _Email = value; }
}
}
In the Main method, we will also instantiate a handful of Customer objects and place them in a list.
var Customers = new List<Customer> {
new Customer {
CustomerID = 7,
Name = "Emma",
Email = "emz0r@worreva.com"
},
new Customer {
CustomerID = 84,
Name = "Pedro",
Email = "pedro@cerveza.es"
},
new Customer {
CustomerID = 102,
Name = "Vladimir",
Email = "vladimir@pivo.ru"
}
};
We would like to produce a list featuring all orders, stating the ID and cost of the order along with the name of the customer. To do this we need to involve both the List of orders and the List of customers in our query. This is achieved using the "join" keyword. Let's replace our query and output code with the following.
// Query.
var Found = from o in Orders
join c in Customers on o.CustomerID equals c.CustomerID
select new { c.Name, o.OrderID, o.Cost };
// Display results.
foreach (var Result in Found)
Console.WriteLine(Result.Name + " spent " +
Result.Cost.ToString() + " in order " +
Result.OrderID.ToString());
The output of running this program is:
Pedro spent 159.12 in order 1
Emma spent 18.5 in order 2
Pedro spent 2.89 in order 3
We use the "join" keyword to indicate that we want to refer to another collection in our query. We then once again use the "in" keyword to declare an identifier that will refer to a single item in the collection; in this case it has been named "c". Finally, we need to specify how the two collections are related. This is achieved using the "on ... equals ..." syntax, where we name a field from each of the collections. In this case, we have stated that the CustomerID of an Order maps to the CustomerID of a Customer.
When the query is evaluated, an object in the Customers collection is located to match each object in the Orders collection. Note that if there were many customers with the same ID, there may be more than one matching Customer object per Order object. In this case, we get extra results. For example, change Vladimir to also have an OrderID of 84. The output of the program would then be:
Pedro spent 159.12 in order 1
Vladimir spent 159.12 in order 1
Emma spent 18.5 in order 2
Pedro spent 2.89 in order 3
Vladimir spent 2.89 in order 3
Notice that Vladimir never featured in the results before, since he had not ordered anything.
Getting All Permutations With Multiple "from"s
It is possible to write a query that gets every combination of the objects from two collections. This is achieved by using the "from" keyword multiple times.
var Found = from o in Orders
from c in Customers
select new { c.Name, o.OrderID, o.Cost };
Earlier I suggested that you could think of "from" as being a little bit like a "foreach". You can also think of multiple uses of "from" a bit like nested "foreach" loops; we are going to get every possible combination of the objects from the two collections. Therefore, the output will be:
Emma spent 159.12 in order 1
Pedro spent 159.12 in order 1
Vladimir spent 159.12 in order 1
Emma spent 18.5 in order 2
Pedro spent 18.5 in order 2
Vladimir spent 18.5 in order 2
Emma spent 2.89 in order 3
Pedro spent 2.89 in order 3
Vladimir spent 2.89 in order 3
Which is not especially useful. You may have spotted that you could have used "where" in conjunction with the two "from"s to get the same result as the join:
var Found = from o in Orders
from c in Customers
where o.CustomerID == c.CustomerID
select new { c.Name, o.OrderID, o.Cost };
However, don't do this, since it computes all of the possible combinations before the "where" clause, which goes on to throw most of them away. This is a waste of memory and computation. A join, on the other hand, never produces them in the first place.
Grouping
Another operations that you may wish to perform is categorizing objects that have the same value in a given field. For example, we might want to categorize orders by CustomerID. The result we expect back is a list of groups, where each group has a key (in this case, the CustomerID) and a list of matching objects. Here's the code to do the query and output the results.
// Group orders by customer.
var OrdersByCustomer = from o in Orders
group o by o.CustomerID;
// Iterate over the groups.
foreach (var Cust in OrdersByCustomer)
{
// About the customer...
Console.WriteLine("Customer with ID " + Cust.Key.ToString() +
" ordered " + Cust.Count().ToString() + " items.");
// And what they ordered.
foreach (var Item in Cust)
Console.WriteLine(" ID: " + Item.OrderID.ToString() +
" Cost: " + Item.Cost.ToString());
}
The output that it produces is as follows:
Customer with ID 84 ordered 2 items.
ID: 1 Cost: 159.12
ID: 3 Cost: 2.89
Customer with ID 7 ordered 1 items.
ID: 2 Cost: 18.5
This query looks somewhat different to the others that we have seen so far in that it does not end with a "select". The first line is the same as we're used to. The second introduces the new "group" and "by" keywords. After the "by" we name the field that we are going to group the objects by. Before the "by" we put what we would like to see in the resulting per-group collections. In this case, we write "o" so as to get the entire object. If we had only been interested in the Cost field, however, we could have written:
// Group orders by customer.
var OrdersByCustomer = from o in Orders
group o.Cost by o.CustomerID;
// Iterate over the groups.
foreach (var Cust in OrdersByCustomer)
{
// About the customer...
Console.WriteLine("Customer with ID " + Cust.Key.ToString() +
" ordered " + Cust.Count().ToString() + " items.");
// And the costs of what they ordered.
foreach (var Cost in Cust)
Console.WriteLine(" Cost: " + Cost.ToString());
}
Which produces the output:
Customer with ID 84 ordered 2 items.
Cost: 159.12
Cost: 2.89
Customer with ID 7 ordered 1 items.
Cost: 18.5
You are not restricted to just a single field or the object itself; you could, for example, instantiate an anonymous type there instead.
Query Continuations
At this point you might be wondering if you can follow a "group ... by ..." with a "select". The answer is yes, but not directly. Both "group ... by ..." and "select" are special in so far as they produce a result. You must terminate a Linq query with one or the other. If you try to do something like:
var CheapOrders = from o in Orders
where o.Cost < 10;
Then it will lead to a compilation error. Since both "select" and "group ... by ..." terminate a query, you need a way of taking the results and using them as the input to another query. This is called a query continuation, and the keyword for this is "into".
In the following example we take the result of grouping orders by customer and then use a select to return an anonymous type containing the CustomerID and the number of orders that the customer has placed.
var OrderCounts = from o in Orders
group o by o.CustomerID into g
select new {
CustomerID = g.Key,
TotalOrders = g.Count()
};
Notice the identifier "g", which we introduce after the keyword "into". This identifier represents an item in the collection containing the results of the previous query. We use in the select statement. Remember that each element of the collection we are querying in this second query is actually a collection itself, since this is what "group ... by ..." produces. Therefore, we can call Count() on it to get the number of elements, which is the number of orders per customer. We grouped by the CustomerID field, so that is our Key.
Query continuations can be used to chain together as many selection and grouping queries as you need in whatever order you need.
Under The Hood
Now we have looked at the practicalities of using Linq, I am going to spend a little time taking a look at how it works. Don't worry if you don't understand everything in this section, it's here for those who like to dig a little deeper.
Throughout the series I have talked about how all of the language features introduced in C# 3.0 somehow help to make Linq possible. While anonymous types have shown up pretty explicitly and you can see from the lack of type annotations we have been writing that there is some type inference going on, where are the extension methods and lambda expressions?
There's a principle in language design and implementation called "syntactic sugar". We use this to describe cases where certain syntax isn't directly compiled, but is first transformed into some other more primitive syntax and then passed to the compiler. This is exactly what happens with Linq: your queries are transformed into a sequence of method calls and lambda expressions.
The C# 3.0 specification goes into great detail about these transformations. In practice, you probably don't need to know about this, but let's look at one example to help us understand what is going on. Our simple query from earlier:
var Found = from o in Orders
where o.CustomerID == 84
select o.Cost;
After transformation by the compiler, becomes:
var Found = Orders.Where(o => o.CustomerID == 84)
.Select(o => o.Cost);
And this is what actually gets compiled. Here the use of lambda expressions becomes clear. The lambda passed to the Where method is called on each element of Orders to determine whether it should be in the result or not. This produces another intermediate collection, which we then call the Select method on. This calls the lambda it is passed on each object and builds up a final collection of the results, which is then assigned to Found. Beautiful, huh?
Finally, a note on extension methods. Both Where and Select, along with a range of other methods, have been implemented as extension methods. The type they use for "this" is IEnumerable, meaning that any collection that implements that interface can be used with Linq. Without extension methods, it would not have been possible to achieve this level of code re-use.
DLinq and XLinq
In this article I have demonstrated Linq working with objects instantiated from classes that we implemented ourselves and stored in built-in collection classes that implement IEnumerable. However, the query syntax compiles down to calls on extension methods. This means that it is possible to write alternative implementations of Linq that follow the same syntax but perform different operations.
Two examples of this, which will ship with C# 3.0, are DLinq and XLinq. DLinq enables the same language integrated query syntax to do queries on databases by translating the Linq into SQL. XLinq enables queries on XML documents.
Conclusion
Linq brings declarative programming to the C# language and will refine and unify the way that we work with objects, databases, XML and whatever anyone else writes the appropriate extension methods for. It builds upon the language features that we have already seen in the previous parts of the series, but hiding some of them away under syntactic sugar. While the query language has a range of differences to SQL, there are enough similarities to make knowledge of SQL useful to those who know it. However, its utility is far beyond providing yet another way to work with databases.
Closing Thoughts On C# 3.0
This brings us to the end of this four part series on C# 3.0. Here is a quick recap on all that we have seen.
Type inference removes much of the tedium of writing out type annotations again and again.
Lambda expressions make higher order programming syntactically light.
Extension methods provide another path to better code re-use when correctly applied.
Object and collection initializers along with anonymous types make building up large data structures much less effort.
Linq gives us declarative programming abilities over objects, databases and XML documents.
When I saw C# 1.0 I highly doubted that C# was going to be a language I would ever be excited about. I have been pleasantly surprised, and writing this series has been a lot of fun. I hope that it has been informative and enjoyable to read, and that it will help you to make powerful use of the new language features. I greatly look forward to being able to use them in my own day-to-day development and seeing how other people use them.
Of course, knowing about something and doing it yourself are two entirely different things; if you haven't already done so, grab yourself the Visual Studio 2008 trial or the free Express Edition. Only then will you become comfortable with the new features and be able to use them effectively in your own development. Happy hacking, and have fun!
# re: Een eerste blik op LINQ
7/8/2009 10:58 AM by xxxxxx
<html>
<head></head>
<body>
<b>test</b>
</body>
</html>
Post Comment
Title
Name
Url
Comment
Remember Me?
ATTENTION: the code you need to copy is CaSe SeNsItIvE and is required to prevent spam.
Enter the code you see:
Copyright © Dennis Vroegop