Verbose WHERE Clause with LINQ to SQL

Table Persons

If you come from the world of always-use-stored-procedures or at least you always write your own SQL query, you’d be curious how LINQ to SQL translates your C# code to SQL query. Say you have table Persons with fields PersonId, FirstName, LastName, and Age. PersonId is the primary key of the table Persons.

In this example, we want to update a particular row (PersonId = 1) with a new Age value. So, we write the following block of code in our application.

Entities.DataContext context = new Entities.DataContext();

int personId = 1;
Entities.Person person =
    context.Persons.SingleOrDefault(p => p.PersonId == personId);
person.Age = 24;
context.SubmitChanges();

To see the generated SQL query, we want to redirect the log of DataContext object to console.

Entities.DataContext context = new Entities.DataContext();
context.Log = Console.Out;

int personId = 1;
Entities.Person person =
    context.Persons.SingleOrDefault(p => p.PersonId == personId);
person.Age = 24;
context.SubmitChanges();

Now, when we run the application, you will see the SQL queries generated by your code in console window. The SQL query will look something similar to the following.

UPDATE [dbo].[Persons]
SET [Age] = @p4
WHERE ([PersonId] = @p0) AND ([FirstName] = @p1) AND ([LastName] = @p2) AND ([Age] = @p3)
-- @p0: Input Int (Size = 0; Prec = 0; Scale = 0) [1]
-- @p1: Input NVarChar (Size = 4; Prec = 0; Scale = 0) [John]
-- @p2: Input NVarChar (Size = 5; Prec = 0; Scale = 0) [Smith]
-- @p3: Input Int (Size = 0; Prec = 0; Scale = 0) [23]
-- @p4: Input Int (Size = 0; Prec = 0; Scale = 0) [24]
-- Context: SqlProvider(Sql2008) Model: AttributedMetaModel Build: 3.5.30729.4926

If you’re used to writing your own query, you probably wonder why the WHERE clause is verbose (or wordy, an English major will say). You see the query tries to find the row to update by matching every single field in the table with their old values. “Well, that’s dumb, ain’t it?” you say. “We can find the row to update by matching only the value of the primary key, PersonId.” Here’s how.

Properties Window

  1. Go to your Solution Explorer in Visual Studio and double click the .dbml file.
  2. Click the property PersonId on the class diagram.
  3. Go to Properties window (it’s on bottom right on default window setup) and find the property Update Check. The current value should be Always.
  4. Change it to Never.

Repeat step 2-4 for all properties (FirstName, LastName, and Age).

Update the application to update the Age to 25 (previously 24). Rebuild and re-run the application. You will see the update statement that you probably always write.

UPDATE [dbo].[Persons]
SET [Age] = @p1
WHERE [PersonId] = @p0
-- @p0: Input Int (Size = 0; Prec = 0; Scale = 0) [1]
-- @p1: Input Int (Size = 0; Prec = 0; Scale = 0) [25]
-- Context: SqlProvider(Sql2008) Model: AttributedMetaModel Build: 3.5.30729.4926

So, what’s the purpose of the property Update Check? The property description says it all. It’s to control the frequency of optimistic concurrency checking.

Posted in Analysis & Design, Implementation | Tagged , , | Leave a comment

It’s Up!

Framing the Sky!

Posted in Framing the Sky, Release | Leave a comment

YouTube Integration Test 1

Posted in Testing, WordPress for Android | Leave a comment

Test Soon and Often

In this imperfect world of browsers inconsistency, you want to test your web pages as frequent as possible while developing them.

Posted in Framing the Sky, Implementation, Testing | Leave a comment

Front Page (Photoshop)

Posted in Analysis & Design, Framing the Sky | Leave a comment

Use Cases (Visitors)

A visitor to the web site (Framing the Sky) will have the ability to browse uploaded photos and leave feedbacks. When arriving at the front page, the visitor will see the latest uploads and their corresponding patterns. The visitor will have the choice to navigate to the next page (older uploads) or view more detailed information of a photo.

  1. Click More Info link below the photo
  2. Hover mouse over the photo to display annotated patterns
  3. Click an oval region
  4. Click thumbs up to indicate that the pattern is indeed used properly

The visitor will have the following abilities as well after selecting a region.

  • Suggest rename pattern
  • Suggest merge pattern

The visitor will have the ability to leave a comment to a photo.

Posted in Analysis & Design, Framing the Sky | Leave a comment

Use Cases (Contributors)

A contributor, given a valid account, will have the ability to upload and annotate a photo. (For the example of this use case, a contributor wants to highlight the strong foreground in the photo.)

  1. Go to Upload page
  2. Select a file to upload
  3. Select Oval tool
  4. Draw an oval on a region of the photo
  5. Select Strong Foreground pattern to associate while the oval region is selected
  6. Publish the photo

Most of the activities a contributor is capable of will be conducted within an administration area where a valid account is required. The following are activities a contributor can perform within the administration area.

  • View uploaded photos
  • Edit published photos
  • Unpublish photos

When annotating a photo, the following are the tools/functionality that can be selected.

  • Draw oval region
  • Draw rectangle region
  • Select region
  • Delete region

When a region is selected, the following functionality will be enabled.

  • Select pattern
  • Deselect pattern
  • Add a pattern
  • Add a note
Posted in Analysis & Design, Framing the Sky | Leave a comment

Front page

The front page will provide site navigation and latest uploads. The photos will be large enough for the visitors to decide whether to view more details of a photo. Below each photo will be a list/cloud of attached principles.

Posted in Analysis & Design, Framing the Sky | Leave a comment

Annotating a Photo

A contributor to the web site (Framing the Sky) will have the ability to upload a photo and add annotation. He will be able to draw shapes over the photo to highlight certain portions of interest on the photo. The highlight is to be labeled with defined composition rules. A highlight can be supplied with a note for further clarification.

Posted in Framing the Sky, Planning | Leave a comment

Framing the Sky

Framing the Sky is a web site that will elaborate why a specific composition or framing is used for a particular photo.

The web site will have contributors that will upload photos and annotate them with common composition patterns/rules that are evident in the photos.

The web site’s visitors will have the ability to learn how a specific photo is taken and to recognize composition patterns. The visitors will be able employ the knowledge in the field.

Posted in Framing the Sky, Planning | Leave a comment