Thursday, August 30, 2007
In LINQ to SQL Part 1 I used a very simple query, almost too simple.  This time I want to take it a step up and add a few joins into the mix, and show how to handle them. Below is a standard TSQL query that will to pull the employee name and his/her territory and region from the Northwind database.  We start all selecting the four columns we want pulled and join several tables together.  Finally, we order the results by lastname and then by firstname. Nothing odd or strange here in TSQL.
 
   1: select e.lastname, e.firstname, t.territoryDescription,r.regiondescription
   2: from employees e
   3: join employeeterritories et on e.employeeid = et.employeeid 
   4: join territories t on t.territoryid = et.territoryid
   5: join region r on r.regionid = t.regionid
   6: order by lastname,firstname
 
In LINQ we once again write our code in somewhat of a reverse order. We start with the from statement and than do our joins statements , followed by the orderby and then select the four columns we want pulled into our resultset. This resultset will be a enumerable anonymous type that can be used with databinding or use with a simple foreach statement.
 
   1: var EmployeeList = from employees in nw.Employees
   2:                    join employeeTerritories in nw.EmployeeTerritories 
   3:                      on employees.EmployeeID equals employeeTerritories.EmployeeID
   4:                    join territories in nw.Territories 
   5:                      on employeeTerritories.TerritoryID equals territories.TerritoryID                               
   6:                    join region in nw.Region 
   7:                      on territories.RegionID equals region.RegionID  
   8:                    orderby employees.LastName,employees.FirstName 
   9:                    select new
  10:                    {
  11:                        employees.LastName,
  12:                        employees.FirstName,
  13:                        territories.TerritoryDescription,
  14:                        region.RegionDescription 
  15:                    };

The following code shows not only the query above, but how simple it is to loop through your resultset and display the information. Remember, when writing your code, you will get the full use of intellisense to help you.

   1: using System;
   2: using System.Collections.Generic;
   3: using System.Linq;
   4: using System.Text;
   5:  
   6: namespace ConsoleTest001
   7: {
   8:  
   9:     class Program
  10:     {
  11:         static void Main(string[] args)
  12:         {
  13:             Northwind nw = new Northwind(Properties.Settings.Default.NorthwindConnectString);
  14:  
  15:             var EmployeeList = from employees in nw.Employees
  16:                                join employeeTerritories in nw.EmployeeTerritories 
  17:                                  on employees.EmployeeID equals employeeTerritories.EmployeeID
  18:                                join territories in nw.Territories 
  19:                                  on employeeTerritories.TerritoryID equals territories.TerritoryID                               
  20:                                join region in nw.Region 
  21:                                  on territories.RegionID equals region.RegionID  
  22:                                orderby employees.LastName,employees.FirstName 
  23:                                select new
  24:                                {
  25:                                    employees.LastName,
  26:                                    employees.FirstName,
  27:                                    territories.TerritoryDescription,
  28:                                    region.RegionDescription 
  29:                                };
  30:                           
  31:             foreach(var e in EmployeeList)
  32:             {
  33:                 Console.WriteLine("{0,-10} {1,-10}\t{2}\t{3,20}", 
  34:                                     e.LastName, 
  35:                                     e.FirstName,
  36:                                     e.TerritoryDescription.PadRight(20,' ').Substring(0,20),
  37:                                     e.RegionDescription);
  38:             }
  39:  
  40:             Console.ReadLine();
  41:  
  42:         }
  43:     }
  44: }

Here is the result of of the LINQ to SQL query written out to the console.

sshot-2

 

kick it on DotNetKicks.com
posted on Thursday, August 30, 2007 2:37:15 PM (Eastern Standard Time, UTC-05:00)  #    Comments [1]


 Wednesday, August 22, 2007
An upcoming new feature of C# 3.0 and part of Visual Studio 2008 ( Orcas ) will be Automatic Properties. This feature is meant to help reduce the number of lines of code a developer has to make and improve the developer experience. Currently, when a developer creates a new class, he has to create a getter and setter for each field. This seems rather redundant when you have no initial setup logic to add, but there are very good reasons such as data binding and future assembly compatibility.  I'm sure most developers have written code similar to the following before:
 
   1: class Customer
   2: {
   3:     private string _FirstName;
   4:     private string _LastName;
   5:  
   6:     public string FirstName
   7:     {
   8:         get
   9:         {
  10:             return _FirstName;
  11:         }
  12:         set
  13:         {
  14:             _FirstName = value;
  15:         }
  16:     }
  17:     public string LastName
  18:     {
  19:         get
  20:         {
  21:             return _LastName;
  22:         }
  23:         set
  24:         {
  25:             _LastName = value;
  26:         }
  27:     }
  28: }

A pretty straight forward customer class with two properties ( FirstName, LastName ).  Now with automatic properties, the same code can be compressed down to fewer lines. When the compiler encounters code like the following, it will automate the generation of the private fields along with get and set method.  This lets the developer write fewer lines of code up front and follow good class design. Later on, if needed, the developer can put custom getter/setter code without having to recompile the consuming assembly.

   1: class Customer
   2: {
   3:     public string FirstName { get; set; }
   4:     public string LastName { get; set; }
   5: }

The last information I've heard is that this as a C# only feature. I haven't heard of anything similar for VB.NET at this time. 

posted on Wednesday, August 22, 2007 1:11:03 PM (Eastern Standard Time, UTC-05:00)  #    Comments [0]


 Tuesday, August 21, 2007

I'm sure everyone so far has at least heard of LINQ. If you haven't, it is Microsoft's new technology for Language Integrated Querying.  LINQ is part of the upcoming 3.5 .Net Framework which will be part of Visual Studio 2008 ( codename Orcas ). LINQ will be able to query in memory objects such as Generics or datasets  in addition to external items like XML documents and SQL databases. As of this post, Visual Studio 2008 is currently in Beta 2 and available for download from Microsoft.

The flavors of LINQ include:

  • LINQ to SQL
  • LINQ to Objects
  • LINQ to  Datasets
  • LINQ  to XML

This will be a LINQ to SQL example so I will be using the standard Northwind database for this article, you will need it installed onto your machine. If you will be using the SQL Express Edition I would also recommend installing SQL Management Studio Express. It will make the management of your database much easier.

The first thing we need to do is to create a DataContext for the our Northwind database. There are two ways to do this: 1. Use SQLMETAL to create a entity class if you need to model a complete database automatically. This way is also useful if you need have a constantly changing scheme since you can have SQLMETAL run each time you build your application using the Pre-Build Event.  2. The second way is to create a DataContext via the LINQ to SQL template under 'Add new Item to Project', but I won't be going into that way in this article.

Open a command prompt and go to your project directory and type:

SQLMETAL /SERVER:ORCASBETA2_VSTS\SQLEXPRESS /DATABASE:NORTHWIND /CODE:NWIND.CS

This will generate a class file that you can add to you project by right clicking Project->Add->Existing Items and selecting the nwind.cs for the file list.  You will next need to add a reference to System.Data.Linq to your project and then your will be ready to add your code. The following code queries the customer table for all records and adds the CustomerName to a listbox on the screen.

   1: namespace Linq001
   2: {
   3:     /// <summary>
   4:     /// Interaction logic for Window1.xaml
   5:     /// </summary>
   6:     public partial class Window1 : Window
   7:     {
   8:         public Window1()
   9:         {
  10:             InitializeComponent();
  11:         }
  12:  
  13:         private void LoadButton_Click(object sender, RoutedEventArgs e)
  14:         {
  15:          
  16:             Northwind nw = new Northwind(@Properties.Settings.Default.NorthwindConnectionString );
  17:  
  18:             var CustomerList = from Customers in nw.Customers
  19:                                select Customers;   
  20:  
  21:             foreach(Customers c in CustomerList)
  22:             {
  23:                 CustomerListBox.Items.Add(c.CompanyName); 
  24:             }
  25:             
  26:         }
  27:     }
  28: }

As you can see, this process is pretty straight forward. Notice the reverse SQL statement order. The from clause comes before the select statement. The result is put into a anonymous type that can be iterated through with a for each loop and added to the listbox.

image

This a extremely simple first example. I will delve into this subject much deeper in the coming weeks.

 

kick it on DotNetKicks.com

posted on Tuesday, August 21, 2007 3:35:51 PM (Eastern Standard Time, UTC-05:00)  #    Comments [0]


 Monday, August 20, 2007

This video was played before the keynote at TechEd 2007.  Funny Stuff.


If you have problem with the above video, you can try here.

posted on Monday, August 20, 2007 12:03:42 PM (Eastern Standard Time, UTC-05:00)  #    Comments [0]


 Tuesday, August 07, 2007

aa700830_VS2008hero_2 

Visual Studio 2008 Beta 2 is out and available for download now. It is sort of funny that beta 1 was named Visual Studio 2007. Anyway, beta 2 can be installed side-by-side to VS 2005. Hopefully, we won't encounter the same removal issue we did with VS 2005.  A go-live license is also included with .NET 3.5.

Some of the coolest new features with Visual Studio 2008 are:

 

  • Multi-Framework targeting support. - Now Visual Studio can target 2.0, 3.0, and the 3.5 framework.
  • New Web Designer with CSS support and Nested Master Pages.
  • ASP.NET Ajax and JavaScript Intellisense and debugging.
  • Language Integrated Query ( LINQ )

 

Those are just some of the new features so go download it can check it out.

posted on Tuesday, August 07, 2007 8:55:58 AM (Eastern Standard Time, UTC-05:00)  #    Comments [0]


 Monday, August 06, 2007

I found this over on Bungie

It’s time for us to sack up and tell you what’s what regarding online co-op.

So you were probably wondering why we didn’t want to commit to two player co-op online over Xbox Live. We certainly got plenty of mail asking, no, demanding that we make it happen. Of course we were working on it, but we were also working on something better. Not two player co-op. Not three-player co-op, but up to four player co-op. Online. On Xbox Live, or sure, System Link if you prefer.

That is correct – up to four player co-op in Campaign mode on Xbox Live or System Link.

We’ve been playing it for quite some time, and bluntly, having some of our greatest ever Halo experiences. We wanted to make sure all our “t”s were crossed and our “i”s dotted before unveiling this important new part of Halo 3’s Campaign mode.

Apart from the horrifying network challenges this new mode presented us, were the various game play balance issues it adds to the fray. So we got it all together, polished it, and that’s why we’re revealing it now. As a result, you’re going to want to play co-op as hard as you can stomach it. We’d certainly recommend Legendary, but we’ll be revealing some more ways to make the game more interesting and challenging for multiple players in the very near future.

Acting as a team is a fantastic new twist to the Campaign game play – something that works as well on foot, in close confines as it does on the battlefield, with large scale vehicle mayhem. Scared of Jackal snipers? Send out a scout to see what dangers lie ahead. Terrified of an open field? Flank your enemies and swipe their rides. The combinations and scenarios are endless – and you’ll be able to enjoy them time and time again in Saved Films of your co-op exploits.

We mentioned that we’ve been having a blast. We have a lot of stories. Many of them would require that we explain some of the reasons replay will be a big factor, but not quite yet. Soon, we promise.

Like every other aspect of Halo 3, this mode is chosen from a Lobby. Simply select Campaign, choose your network (Xbox Live or System Link) and invite your friends to join you, or they can simply join your session once it’s set up and in a lobby (but they cannot join a game once it’s in-progress). They will pop into your lobby instantly, and when you have all the players you need (anywhere from one to four) you can begin the game from the beginning – or from any of your saved checkpoints.

Split-screen co-op is as ever, limited to two players per screen – but they can if they wish join two other friends online or via System Link.

For fictional reasons, we decided to avoid Halo’s slightly surreal compromise of two identical Master Chiefs and rather, use this opportunity to expand the Halo universe just a little bit. Player one will control the Master Chief, player two will control the Arbiter and players three and four will get the chance to control two brand new Elite characters.

posted on Monday, August 06, 2007 4:12:31 PM (Eastern Standard Time, UTC-05:00)  #    Comments [2]


 Friday, July 13, 2007

DevExpress is making Refactor for ASP.NET freely available to all ASP.NET 2.0 developers as an add-in to Visual Studio 2005 and Orcas, so if you don't have the Professional version yet, go download this free version of the product.

Included Refactorings

Add Validator
Create Overload
Encapsulate Field
Extract ContentPlaceHolder
Extract ContentPlaceHolder
Extract Method
Extract Property
Extract Style (Class)
Extract Style (id)
Extract to User Control
Flatten Conditional
Inline Temp
Introduce Constant
Introduce Local
Introduce Local (replace all)
Move Declaration Near Reference
Move Initialization to Declaration
Move Style Attributes to CSS
Move to Code-behind
Rename
Reorder Parameters
Replace Temp with Query
Reverse Conditional
Safe Rename
Simplify Expression
Split Initialization from Declaration
Split Temporary Variable
Surround with Update Panel
posted on Friday, July 13, 2007 1:58:27 PM (Eastern Standard Time, UTC-05:00)  #    Comments [0]


Looks like this mini-series is going to end with just four episodes. That kind of sucks, but they were funny.

 

posted on Friday, July 13, 2007 1:55:05 PM (Eastern Standard Time, UTC-05:00)  #    Comments [0]