Open Visual Studio 2005 and select File -> New ->
Project and select the option LINQ Windows Application. Name the Project and
For creating the windows application, I have created the
following database with two tables as Project and Requirement with the fields
Now select the project, right click and select Add -> New
Item. Then select DLinqObjects from the List of Items available. The
DLinqObject gets added to the project and the designer surface opens.
Expand the Server Explorer, open the database and expand the
tables. Select the table "Project" from the list and then drag and drop
it on the surface of the DLinq designer. Build the project after dropping the
object on the design surface. Now if you open the DLinqObjects1.cs file, you
can see the following code in it.
public partial class @__SQLEXPRESSDataContext: System.Data.DLinq.DataContext
public System.Data.DLinq.Table < Project > Projects;
public System.Data.DLinq.Table < Requirement > Requirements;
[System.Data.DLinq.Table(Name = "Project")]
public partial class Project: System.Data.DLinq.INotifyPropertyChanging,
private int _ProjectID;
private string _ProjectName;
private string _ProjectDesc;
private System.DateTime _StartDate;
private System.DateTime _EndDate;
private int _GroupID;
private System.Nullable < bool > _ActiveFlag;
private System.Data.DLinq.EntitySet < Requirement > _Requirements;
this._Requirements = new System.Data.DLinq.EntitySet < Requirement > (new
System.Data.DLinq.Notification < Requirement > (this.Attach_Requirements),
new System.Data.DLinq.Notification < Requirement >
(Name = "ProjectID", Storage = "_ProjectID", DBType =
"int NOT NULL IDENTITY", Id = true, AutoGen = true)]
public virtual int ProjectID
if ((this._ProjectID != value))
this._ProjectID = value;
The @__SQLEXPRESSDataContext is the connection object for
the database connection. I used SQLExpress for the example. You can see the code
for the entity table "Project" and the columns added to the designer.
I have not given the code for the other columns as it is similar to the field
Select the menu Data -> Add New Data Sources in Visual
Studio. This will open the wizard for adding data sources. The first step of
the wizard will ask you to select the type of the source that will provide data
to the application. Select Object from the options.
The next step will list the available objects for binding
the controls. As we have the Projects entity class on the designer, this will
get listed under the project name. Select the Object "Project,"
select "next" and then finish the wizard.
Now you can see the data source added to the project under
the Data Sources folder. You can also view the properties of the data source
by selecting the menu Data -> Show DataSources as shown below.
Now we are ready to Design the windows form. Open the form design
surface. On the Data Sources explorer select the Project Datasource. You can
see the following options when you select the drop down next to the Data Source
"Project." This is to ease the design of the user Interface. You
can choose how you want to present the UI to the user. For this example I have
selected Details View.
Now drag and drop the Project Data Source on the form design
surface. The Visual Studio places the corresponding data controls on the form
according to the data type of the column/properties of the Table/Entity class. We
do not need to select the controls and bind it manually. It is all done by the
Visual Studio. You can also see a navigator on top of the form. This gives
the flexibility of navigation on the records and also provides the editing
controls in the navigation bar. This helps us reduce the design time.
Add the following code to the form file to provide the
necessary connections to the DataSource and then bind it. The con is the
connection object created using the "@__SQLEXPRESSDataContext" connection
that we saw earlier.
public partial class Form1: Form
private@__SQLEXPRESSDataContext con = new@__SQLEXPRESSDataContext();
private void Form1_Load(object sender, EventArgs e)
projectBindingSource.DataSource = con.Projects.ToBindingList();
Now save the project, build it and execute it. You can see
the below UI with the navigation feature also in it. The Save button is disabled
as we have not enabled it nor have we added the code for it. Before going for
the editing feature, we will add another data source and a detail control for
it. Let us make this application as Master Detail application.
Now close the application and go to the design to open the
DLinqObject Design Surface. Now drag and drop the second table
"Requirement" from the server explorer onto the Designer surface. You
can see the following on the design surface. The Designer creates the
Association relationship between the two entities.
Add data source for the "Requirement" Object just
as we did previously for the project entity. Use the Data Sources wizard for
Now open the Data sources explorer and expand it. You can
see Requirements as one of the properties under the Project Data Source. This
means that the Requirement is one of the details of the project and the Requirement
itself is an object with its own properties.
Using the drop down next to the "Requirements"
property, set the view as "DataGridView." This allows us to see the
details in a grid when we navigate through the project records.
Drag and drop the Requirements object from the Data Sources
explorer onto the form design surface. Select the Save button in the navigator
and open the properties window. Using the properties window, enable the save
button. Add the following code for the click event of the Save button.
private void projectBindingNavigatorSaveItem_Click(object sender, EventArgs e)
Save the project and execute it. Now try navigating
through the records; try editing and saving the changes.
The next step we can take is to create the search option on
the Project name. This is very simple to achieve by using the Linq query. Let
us see how we can do it.
Add a button next to the project name text box. Name it whatever
you would like. Here I named it as "Find." Add the following code
to the Click event of the Find button.
private void FindButton_Click(object sender, EventArgs e)
var projs = from projects in con.Projects where projects.ProjectName ==
projectNameTextBox.Text select projects;
projectBindingSource.DataSource = projs.ToBindingList();
Save and execute the project. Now enter a valid project
name in the project name text box and then click the "Find" button. This
will bring you the details of the project name entered and also the
corresponding Requirement details in the grid.