Theme Graphic
Theme Graphic

The Official Programmer's Heaven Blog

The blog where the Programmer's Heaven team post stuff.

Subscribe

Author

Often knowledgable, sometimes wise, occasionally funny. The Programmer's Heaven blog team post about a whole range of topics, from practical advice on concurrency control to introductions to lesser known concepts such as functional programming. Don't forget to comment on the posts and let them know what you think, like and hate!

Archive

Tags

Posted on Friday, March 14, 2008 at 9:10 AM

Very Quick Guide To DLinq: Part 1


We have already written about using Linq, but didn't cover DLinq. This post takes a quick look at getting started with DLinq, which allows you to write Linq queries against databases. Once you're up to speed, you can take a database and be writing Linq queries against it in under five minutes!

In future parts in this series of posts, we'll look at doing database updates, writing more complex queries and debugging. In this one, we'll just look at doing some simple queries.

Creating The Linq to SQL Classes

The bad news is that before you can use DLinq, you have to create Linq to SQL classes to represent your database tables. The good news is that Visual Studio 2008 (aside from the Express Edition) can do all of the hard work for you in this.

To get started, right click in the Solution Explorer in your project (or a directory in your project), go to the Add sub-menu and choose "New Item...". In the resulting dialog, choose the "Data" category to the left, then "LINQ to SQL Classes" in the resulting list on the right. Enter a name for the group of classes (collectively known as a Data Context), and press OK.



Next, you need to connect to the database holding the tables that you want to do queries on. To do this, open the Server Explorer (if you don't see it, it's on the View Menu). Right-click on "Data Connections", then choose "Add Connection..." Fill out the fields in the resulting dialog, and click OK. You will then have your database in the list.



Open up the Tables subtree beneath the connection you just created. Select the tables that you want to be able to do queries on (select multiple by holding down Control or Shift). Then drag them to the left pane of the data context. Visual Studio will then create the classes for these tables, and you'll see the tables represented in the data context.



Finally, click in the data context somewhere other than on a table. Go to the properties window and set the namespace and the class name for the class that represents the data context as a whole.



Finally, save this.

Writing Queries

For the sake of this article, I've just created DLinq classes for a couple of bits of the Programmer's Heaven resources database, where we store details of files, links, articles, books and so forth. I'm using a Console Application to play with this.

First, we need to instantiate the data context.
var DB = new ResourcesDB();
The members of DB represent, amongst other things, the tables in the database. We can use these to write queries on those tables.
var Result = from Book in DB.BookResources
             where Book.Published == "1997"
             select Book;
Remember that we use C# syntax rather than SQL syntax; it's all to easy to forget this and write SQL "=" rather than C# "==". Thankfully, if you make this mistake, the compiler will complain. Of course, you don't have to just select the Book; you can instantiate and populate any class that you wish to instead.

Getting At The Query Results

In the previous query we selected all books in our database published in 1997. We can now iterate over the Result collection and print the title of the books.
foreach (var Found in Result)
    Console.WriteLine(Found.Title);
Which produces a list of titles of the books that were found.

The Next Steps

If you have already read our Linq tutorial, you'll have seen how you can write some pretty complex queries over collections of objects. All of that same power is available in DLinq too, so if you want to learn how to write more complex queries then you should refer to that. In future parts of this series, I will look at other DLinq specific bits.
Tags: .NET, C#, DLinq
Bookmark: Submit To Digg Submit To reddit Submit To del.icio.us Bookmark With StumbleUpon Bookmark With FaceBook Bookmark With Google Bookmarks   Share: Share By Email By Email

4 comments on "Very Quick Guide To DLinq: Part 1"
Posted by http://vredit.wikido on Monday, May 12, 2008 at 4:25 PM
Image Of Author
http://vredit.wikidot.com/free-credit-report
http://vredit.wikidot.com/free-credit-report http://vredit.wikidot.com/free-annual-credit-report http://vredit.wikidot.com/free-credit-report-com http://vredit.wikidot.com/free-online-credit-report http://vredit.wikidot.com/my-free-credit-report http://vredit.wikidot.com/free-credit-report-on-line http://vredit.wikidot.com/free-credit-report-and-score http://vredit.wikidot.com/free-credit-report-government http://vredit.wikidot.com/free-credit-score-report http://vredit.wikidot.com/free-anual-credit-report http://vredit.wikidot.com/get-a-free-credit-report http://vredit.wikidot.com/www-free-credit-report-com http://vredit.wikidot.com/get-free-credit-report http://vredit.wikidot.com/free-yearly-credit-report http://vredit.wikidot.com/free-credit-reports http://vredit.wikidot.com/credit-report-for-free http://vredit.wikidot.com/experian-free-credit-report http://vredit.wikidot.com/free-annual-credit-report-com http://vredit.wikidot.com/free-credit-report-no-credit-card http://vredit.wikidot.com/free-credit-report-gov http://vredit.wikidot.com/free-copy-of-credit-report http://vredit.wikidot.com/free-instant-credit-report http://vredit.wikidot.com/free-credit-report-no-credit-card-required http://vredit.wikidot.com/totally-free-credit-report http://vredit.wikidot.com/free-annual-credit-reports http://vredit.wikidot.com/free-credit-reports-online http://vredit.wikidot.com/how-to-get-a-free-credit-report http://vredit.wikidot.com/my-free-credit-report-com http://vredit.wikidot.com/your-free-credit-report
Posted by dfg on Sunday, May 18, 2008 at 11:15 PM
Image Of Author
sdfghj
http://mortage.wikidot.com/mortgage-calculator http://mortage.wikidot.com/flash-mortgage-calculator http://mortage.wikidot.com/mortgage-calculator-plus http://mortage.wikidot.com/complete-mortgage-calculator http://mortage.wikidot.com/mortgage-calculator-com http://mortage.wikidot.com/www-mortgage-calculator http://mortage.wikidot.com/mortgage-calculator-for-website http://mortage.wikidot.com/mortgage-calculator-software http://mortage.wikidot.com/free-mortgage-calculator http://mortage.wikidot.com/c-mortgage-calculator http://mortage.wikidot.com/business-mortgage-calculator http://mortage.wikidot.com/mortgage-calculator-download http://mortage.wikidot.com/mortgage-calculators http://mortage.wikidot.com/free-mortgage-calculators http://mortage.wikidot.com/aol-mortgage-calculator http://mortage.wikidot.com/quick-mortgage-calculator http://mortage.wikidot.com/calc-mortgage http://mortage.wikidot.com/mortgage-calculator-html http://mortage.wikidot.com/calculate-your-mortgage http://mortage.wikidot.com/financial-mortgage-calculator http://mortage.wikidot.com/mortgage-calculator-new-york http://mortage.wikidot.com/easy-mortgage-calculator http://mortage.wikidot.com/mortgage-rates-calculator http://mortage.wikidot.com/calculator-for-mortgage http://mortage.wikidot.com/detailed-mortgage-calculator http://mortage.wikidot.com/canadian-mortgage-calculator http://mortage.wikidot.com/mortgage-finance-calculator http://mortage.wikidot.com/california-mortgage-calculator http://mortage.wikidot.com/mortgage-calculator-table http://mortage.wikidot.com/bi-weekly-mortgage-calculators

http://allrecipes.wikidot.com/recipe http://allrecipes.wikidot.com/chicken-recipe http://allrecipes.wikidot.com/salmon-recipe http://allrecipes.wikidot.com/recipes http://allrecipes.wikidot.com/chicken-recipes http://allrecipes.wikidot.com/all-recipes http://allrecipes.wikidot.com/cake-recipes http://allrecipes.wikidot.com/cheesecake-recipe http://allrecipes.wikidot.com/cookie-recipe http://allrecipes.wikidot.com/cake-recipe http://allrecipes.wikidot.com/smoothie-recipe http://allrecipes.wikidot.com/salad-recipes http://allrecipes.wikidot.com/food-recipes http://allrecipes.wikidot.com/drink-recipes http://allrecipes.wikidot.com/recipes-com http://allrecipes.wikidot.com/a-recipe http://allrecipes.wikidot.com/food-recipe http://allrecipes.wikidot.com/cookie-recipes http://allrecipes.wikidot.com/dessert-recipes http://allrecipes.wikidot.com/salsa-recipe http://allrecipes.wikidot.com/healthy-recipes http://allrecipes.wikidot.com/easy-recipes http://allrecipes.wikidot.com/pancake-recipe http://allrecipes.wikidot.com/cocktail-recipe http://allrecipes.wikidot.com/soup-recipes http://allrecipes.wikidot.com/salmon-recipes http://allrecipes.wikidot.com/chili-recipe http://allrecipes.wikidot.com/crock-pot-recipes http://allrecipes.wikidot.com/dinner-recipes http://allrecipes.wikidot.com/bread-recipes
Posted by wer on Sunday, May 18, 2008 at 11:17 PM
Image Of Author
wer
http://mortage.wikidot.com/mortgage-calculator http://mortage.wikidot.com/flash-mortgage-calculator http://mortage.wikidot.com/mortgage-calculator-plus http://mortage.wikidot.com/complete-mortgage-calculator http://mortage.wikidot.com/mortgage-calculator-com http://mortage.wikidot.com/www-mortgage-calculator http://mortage.wikidot.com/mortgage-calculator-for-website http://mortage.wikidot.com/mortgage-calculator-software http://mortage.wikidot.com/free-mortgage-calculator http://mortage.wikidot.com/c-mortgage-calculator http://mortage.wikidot.com/business-mortgage-calculator http://mortage.wikidot.com/mortgage-calculator-download http://mortage.wikidot.com/mortgage-calculators http://mortage.wikidot.com/free-mortgage-calculators http://mortage.wikidot.com/aol-mortgage-calculator http://mortage.wikidot.com/quick-mortgage-calculator http://mortage.wikidot.com/calc-mortgage http://mortage.wikidot.com/mortgage-calculator-html http://mortage.wikidot.com/calculate-your-mortgage http://mortage.wikidot.com/financial-mortgage-calculator http://mortage.wikidot.com/mortgage-calculator-new-york http://mortage.wikidot.com/easy-mortgage-calculator http://mortage.wikidot.com/mortgage-rates-calculator http://mortage.wikidot.com/calculator-for-mortgage http://mortage.wikidot.com/detailed-mortgage-calculator http://mortage.wikidot.com/canadian-mortgage-calculator http://mortage.wikidot.com/mortgage-finance-calculator http://mortage.wikidot.com/california-mortgage-calculator http://mortage.wikidot.com/mortgage-calculator-table http://mortage.wikidot.com/bi-weekly-mortgage-calculators

http://allrecipes.wikidot.com/recipe http://allrecipes.wikidot.com/chicken-recipe http://allrecipes.wikidot.com/salmon-recipe http://allrecipes.wikidot.com/recipes http://allrecipes.wikidot.com/chicken-recipes http://allrecipes.wikidot.com/all-recipes http://allrecipes.wikidot.com/cake-recipes http://allrecipes.wikidot.com/cheesecake-recipe http://allrecipes.wikidot.com/cookie-recipe http://allrecipes.wikidot.com/cake-recipe http://allrecipes.wikidot.com/smoothie-recipe http://allrecipes.wikidot.com/salad-recipes http://allrecipes.wikidot.com/food-recipes http://allrecipes.wikidot.com/drink-recipes http://allrecipes.wikidot.com/recipes-com http://allrecipes.wikidot.com/a-recipe http://allrecipes.wikidot.com/food-recipe http://allrecipes.wikidot.com/cookie-recipes http://allrecipes.wikidot.com/dessert-recipes http://allrecipes.wikidot.com/salsa-recipe http://allrecipes.wikidot.com/healthy-recipes http://allrecipes.wikidot.com/easy-recipes http://allrecipes.wikidot.com/pancake-recipe http://allrecipes.wikidot.com/cocktail-recipe http://allrecipes.wikidot.com/soup-recipes http://allrecipes.wikidot.com/salmon-recipes http://allrecipes.wikidot.com/chili-recipe http://allrecipes.wikidot.com/crock-pot-recipes http://allrecipes.wikidot.com/dinner-recipes http://allrecipes.wikidot.com/bread-recipes
Posted by Hermes Birkin on Friday, July 27, 2012 at 5:39 PM
Image Of Author
Hermes Handbags
Hermes Handbags

Leave A Comment
Subject:


Comment:
   Bold Italic Underline          Code Link Image Horizontal Rule


Because you do not have or are not logged in to your Programmer's Heaven account, please enter your name.

Name:


To help prevent comment SPAM, please enter the magic code '378' in the box:




Posting Rules
Please follow these rules when posting comments on blog posts.
  • Do not post anything that is racist, hate speech or of a sexual or adult nature.
  • Do not post or link to anything that infringes copyrighted laws.
  • Posting about security or legal topics is fine so long as you are not glorifying or encouraging people to perform illegal activities.
  • Both the author of this blog and the Programmer's Heaven administrators may delete any inappropriate comments without notice at their own discretion.
 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.