Monday, September 10, 2007 3:05 PM tony_lombardo

First Crack at LINQ and Visual Studio 2008 (Codename Orcas)

I started working with VS2008 pretty regularly about a month ago.  One of the first things I wanted to try out was LINQ.  I had been to assorted sessions at PDC, TechEd, MIX, and some other shows where they covered LINQ, and I was pretty impressed.  In the past, figuring out how to use a new VS feature was almost like a treasure hunt.  You could pick up clues here and there, but putting it all together was challenging.  I'm not sure if it's due to Scott Gu's blogs, or the intuitive nature of LINQ (or both), but I was pleasantly surprised that I could do it! 

Using the LINQ to SQL classes, I created a DataModel which mapped to my SQL tables.  It was then just a bit of simple LINQ to bind the records to my table.  Basically in 20 minutes I had created a LINQ based data structure and hooked up my tables to create a master details view.  Now that's not to say that I've mastered LINQ, but that does speak to it's intuitive nature. 

My back end data was the Adventure Works database served up from SQL.  Here's a look at my DataBinding code:

DataClassesDataContext context = new DataClassesDataContext();
var employees = from emp in context.Employees
                select new
                {
                    EmployeeID = emp.EmployeeID,
                    Name = emp.Contact.LastName + ", " + emp.Contact.FirstName,
                    BirthDate = emp.BirthDate
                };
this.GridView1.DataSource = employees;
this.GridView1.DataKeyNames = new string[]{"EmployeeID"};
this.GridView1.DataBind();

And then my detailsview code:

DataClassesDataContext context = new DataClassesDataContext();
var contact = from c in context.Employees
               where c.EmployeeID == (int)this.GridView1.SelectedDataKey.Value
               select c.Contact;
this.DetailsView1.DataSource = contact;
this.DetailsView1.DataBind();

Something worth pointing out - in the first snippet, notice that I'm doing a "select new"  I'm using an anonymous type here so that I don't have to stub out a class just to represent my data.  This feature is great.  I always thought there must be a better way than stubbing out dummy classes just to act as my business objects.  Now that's not to say that there's no need for creating separate business object classes anymore, but this makes a nice alternative for the simpler cases, where a full business object layer may be overkill.   

Filed under: ,

Comments

# re: First Crack at LINQ and Visual Studio 2008 (Codename Orcas)

Monday, September 10, 2007 6:20 PM by Timothy Khouri

Yeah, I'm loving LINQ myself!

I've gotten two new major projects at my job, both of which I'm speeding through (at least, going much faster through) due to using LINQ :)

# re: First Crack at LINQ and Visual Studio 2008 (Codename Orcas)

Monday, September 10, 2007 9:34 PM by SixYo

It is very helpful for me . Thanks.

# re: First Crack at LINQ and Visual Studio 2008 (Codename Orcas)

Wednesday, September 12, 2007 2:42 PM by tony_lombardo

Hi Tim, that's exactly how I felt.. it caught me by surprise how productive I could be with something so new.  

Six - glad you found this helpful!

# re: First Crack at LINQ and Visual Studio 2008 (Codename Orcas)

Thursday, February 17, 2011 2:48 AM by Jane Swingfield

It was rather interesting for me to read this article. Thanx for it. I like such topics and everything connected to them. I definitely want to read more soon.    

Jane  Swingfield  

<a href="indianescortmodels.com/">indian escorts in west london</a>

# re: First Crack at LINQ and Visual Studio 2008 (Codename Orcas)

Thursday, February 24, 2011 5:34 PM by Natasha Stepman

It was extremely interesting for me to read the blog. Thanx for it. I like such topics and anything that is connected to them. I definitely want to read a bit more on this site soon.    

Natasha  Stepman    

<a href="www.latinescortlondon.com/">latin London escorts</a>

# re: First Crack at LINQ and Visual Studio 2008 (Codename Orcas)

Friday, March 04, 2011 11:24 AM by Whitny Kripke

It is extremely interesting for me to read the post. Thanks for it. I like such topics and everything that is connected to them. I would like to read a bit more on this site soon.      

Whitny  Kripke    

<a href="www.jammer-store.com/">jammer 5</a>

# re: First Crack at LINQ and Visual Studio 2008 (Codename Orcas)

Friday, April 01, 2011 5:09 PM by Natasha Flouee

It was extremely interesting for me to read that article. Thanks for it. I like such themes and anything connected to them. I would like to read a bit more on this site soon.          

Natasha  Flouee          

<a href="milanescorts.com/">tour girl milano escort</a>

# re: First Crack at LINQ and Visual Studio 2008 (Codename Orcas)

Friday, April 15, 2011 8:42 PM by Whitny Flouee

It was rather interesting for me to read this blog. Thanks for it. I like such topics and everything connected to them. I would like to read a bit more soon.            

Whitny  Flouee          

<a href="www.baccaratgirls.com/">escorts incall</a>

# re: First Crack at LINQ and Visual Studio 2008 (Codename Orcas)

Monday, April 25, 2011 3:01 AM by Whitny Kripke

It is extremely interesting for me to read this article. Thank author for it. I like such topics and everything connected to this matter. I definitely want to read a bit more soon.            

Whitny  Kripke            

<a href="monacoescort.com/">lady escort monaco</a>

Leave a Comment

(required) 
(required) 
(optional)
(required)