My LINQ Cheatsheet

I've been banging .NET 3.5 lately and this rendezvouz with LINQ (Language Integrated Query) has been making my brain smile alot. I mean, how can you not like something so easy and well defined and makes your life as developer alot easier.

Anyhow, I just wanted to post my LINQ cheat sheet. It's not much and it's not even complete yet (I call this part 1.1). It consist of a few snippets that you might commonly do when doing LINQ processing. I'm planning to update this and hopefully I could find time this week to add some more piece of code that would demonstrate the other LINQ topics that I missed.

Anyway, here's some LINQ snippets:

//create our PersonService object
PersonService ps = new PersonService();

//get the list of person
PersonList listOfPerson = ps.GetPersonList();

//get a list of people with Gender set to Male
var maleOnlyList =
from l
in listOfPerson
where l.Gender == Gender.Male
select l;

//get a list of people with Gender set to female
//and declaring the returned fields
var femaleOnlyList =
from l
in listOfPerson
where l.Gender == Gender.Female
select new {
l.PersonID,
l.FirstName,
l.MiddleName,
l.LastName,
l.Email,
l.BirthDate,
l.Gender,
l.DateCreated };

//specify age
int selectedAge = 25;
//get a list of people with age greater than 25
var ageIsGreaterThan25 =
from l
in listOfPerson
where l.BirthDate >= DateTime.Now.AddYears(-selectedAge)
select l;

//get a list of people with a birthdate between a specified range
var birthdateBetweenRange =
from l
in listOfPerson
where
l.BirthDate >= DateTime.Parse("1/1/1980")
&& l.BirthDate <= DateTime.Parse("1/1/1981")
select l;


//order the result by lastname
var orderByLastNameSimple =
from l
in listOfPerson
orderby l.LastName
select l;

//order the list by birthdate and lastname
var orderByMultiple =
from l
in listOfPerson
orderby l.BirthDate descending, l.LastName ascending
select l;

//take three records
var takeThree = listOfPerson.Take(3);

//go to the 10th record and then take 3 records from there
var skipTenTakeThree = listOfPerson.Skip(10).Take(3);

//skip up until the Lastname is not equal to Thornton
var skipWhile = listOfPerson.SkipWhile(n => n.LastName != "Thornton");

P.S: There's more LINQ examples at the MSDN website.


Posted Dec 17 2007, 12:30 PM by keithrull

Comments

marl wrote re: My LINQ Cheatsheet
on 12-17-2007 1:15 PM

this takes on reducing the difficulty of embedded sql? is that right?

The Visitor wrote re: My LINQ Cheatsheet
on 12-17-2007 3:13 PM

I really hate Microsoft when they are upgrading their programming language... They tend to change it all again... Like what they did to VB6

cruizer wrote re: My LINQ Cheatsheet
on 12-17-2007 9:51 PM

i thinq (ha ha!) that LINQ is a good thing. from what i know something like it will also be included in a future version of java.

lamia wrote re: My LINQ Cheatsheet
on 12-18-2007 2:13 AM

Is that the so-called "closures"?

jakelite wrote Closures in Linq
on 12-18-2007 12:32 PM

Keith posted a great article demonstrating Linqs capabilities. There he was asked if his examples where

jakelite wrote re: My LINQ Cheatsheet
on 12-18-2007 12:33 PM
keithrull wrote re: My LINQ Cheatsheet
on 01-02-2008 8:57 AM

Nice explanation jakelite!

jakelite wrote Closures and Linq in C# 3.0
on 07-23-2008 8:21 PM

Keith posted a great article demonstrating Linqs capabilities. There he was asked if his examples where

Add a Comment

(required)  
(optional)
(required)  
Remember Me?

Enter the numbers above:

Copyright DevPinoy 2005-2008