Converting SQL to LINQ, Part 3: DISTINCT, WHERE, ORDER BY and Operators (Bill Horst)

Published 04 December 07 10:31 AM

This post assumes you’ve read the previous posts in this series:

Converting SQL to LINQ, Part 1: The Basics

Converting SQL to LINQ, Part 2: FROM and SELECT

Continuing with specific query clauses, I will cover topics related to DISTINCT, WHERE and ORDER BY.

DISTINCT

 

SQL SELECT statements can include the DISTINCT specifier, which causes all duplicate records in the query result to be removed.  In a LINQ expression, Distinct is its own individual clause, rather than a specifier on the Select clause.  This means that Distinct can appear between any two other clauses.  The Distinct clause takes whatever result is returned by the preceding clause (Select, in the case below) and returns a filtered result with duplicates removed.  To two code examples below accomplish the same results:

 

SQL

SELECT DISTINCT Name, Address

FROM CustomerTable

 

 

VB

From Contact In CustomerTable _

Select Contact.Name, Contact.Address _

Distinct

 

 

WHERE

 

Much like a SQL query, a LINQ expression allows you to add a Where clause to filter the results based on a certain condition.  You can use any valid VB Boolean expression.

 

SQL

SELECT * FROM CustomerTable

WHERE State = “WA”

 

 

VB

From Contact In CustomerTable _

Where Contact.State = “WA”

 

 

Operators

 

SQL WHERE clauses can often include other operators like AND.  There will usually be a similar operator in VB that can be used in the Where clause expression to accomplish the same result.

 

SQL

SELECT * FROM CustomerTable

WHERE City = “Seattle” AND Zip = “98122”

 

 

VB

From Contact In CustomerTable _

Where Contact.City = “Seattle” And Contact.Zip = “98122”

 

 

Even if there is no equivalent VB operator, it should be possible to represent nearly any SQL WHERE expression as a VB expression.  BETWEEN is an example of a SQL keyword that doesn’t have an analogous VB keyword, but which can be easily represented in VB.

 

SQL

SELECT * FROM OrderTable

WHERE OrderDate BETWEEN ‘Sept-22-2007’ AND ‘Sept-29-2007’

 

 

VB

From Shipment In OrderTable _

Where (Shipment.OrderDate > #9/22/2007#) _

    And (Shipment.OrderDate < #9/29/2007#)

 

 

ORDER BY

 

The SQL ORDER BY clause can also be represented in a LINQ expression.  A LINQ Order By clause allows for a comma-delimited list of expressions to specify how results should be sorted.  Any valid VB expression can be used, so these expressions don’t necessarily have to be the names of field that were selected.

 

SQL

SELECT * FROM CustomerTable

ORDER BY Phone

 

 

VB

From Contact In CustomerTable _

Order By Contact.Phone

 

 

ASC/DESC

A SQL ORDER BY clause can also include ASC and DESC keywords, to specify that the sort should be in ascending or descending order, respectively.  VB uses Ascending and Descending keywords for the same purpose, with the same syntax.  If neither specifier is present, ascending order is the default.

 

SQL

SELECT * FROM CustomerTable

ORDER BY Phone ASC, Name DESC

 

 

VB

From Contact In CustomerTable _

Order By Contact.Phone Ascending, Contact.Name Descending

 

 

With this and my two previous posts, it should be possible to convert basic SQL queries to VB LINQ code.  Next week, I’ll talk about Functions, both Scalar and Aggregate.

-      Bill Horst, VB IDE Test

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

# Free People Searches » Converting SQL to LINQ, Part 3: DISTINCT, WHERE, ORDER BY and Operators (Bill Horst) said on December 4, 2007 3:28 PM:

PingBack from http://www.absolutely-people-search.info/?p=3007

# Jorge Serrano - MVP Visual Developer - Visual Basic said on December 5, 2007 1:16 AM:

Si quieres aprender LINQ para Visual Basic 2008, quizás te interesen los siguientes artículos que el

# The Visual Basic Team said on December 12, 2007 1:35 PM:

This post assumes you’ve read the previous posts in this series: Converting SQL to LINQ, Part 1: The

# The Visual Basic Team said on December 18, 2007 7:09 PM:

This post assumes you’ve read the previous posts in this series: Converting SQL to LINQ, Part 1: The

# El blog de Jorge said on December 20, 2007 2:16 AM:

Si quieres aprender LINQ para Visual Basic 2008, quizás te interesen los siguientes artículos que el

# jackie said on December 20, 2007 10:21 AM:

Hi,

I was wondering what would be the VB statement if I would like to select, where the OrderDate is, say in the last 3 months ? If I do not want to put the dates in the statement.

jackie

# VBTeam said on December 31, 2007 1:04 PM:

Hi Jackie,

You can use the Now keyword to get the current date/time.  There are probably several ways to code up this query, but the way I would do it is like this:

Dim ThreeMonthSpan As New TimeSpan(90,0,0,0) '90 days

Dim OrdersInLastThreeMonths = From Shipment In OrderTable _

Where Shipment.OrderDate >= Now.Subtract(ThreeMonthSpan)

Hope that helps,

 - Bill

# The Visual Basic Team said on December 31, 2007 1:49 PM:

This post assumes you’ve read the previous posts in this series: Converting SQL to LINQ, Part 1: The

# Noticias externas said on December 31, 2007 1:57 PM:

This post assumes you’ve read the previous posts in this series: Converting SQL to LINQ, Part 1: The

# zack said on January 3, 2008 9:42 PM:

引用: 从SQL到LINQ,Part3:DISTINCT,WHERE,ORDERBYandOperators(BillHorst) [原文作者]:BillHorst...

# The Visual Basic Team said on January 8, 2008 2:35 PM:

This post assumes you’ve read the previous posts in this series: Converting SQL to LINQ, Part 1: The

# Noticias externas said on January 8, 2008 3:33 PM:

This post assumes you’ve read the previous posts in this series: Converting SQL to LINQ, Part 1: The

# Hot Topics said on February 10, 2008 7:14 PM:

Bill Horst of the VIsual Basic team at Microsoft has been writing a great series on Converting SQL to

# The Visual Basic Team said on May 29, 2008 3:46 AM:

ここでは、このシリーズの前の投稿をお読みになっていることを前提としています。 Converting SQL to LINQ, Part 1: The Basics ( 英語 ) Converting SQL

# Michael Hagesfeld said on February 26, 2009 11:03 AM:

Is it possible, given the VB lambda iossues, to do an order by on a distinct query?  (select x order by y).distinct will override the order by

Leave a Comment

(required) 
(optional)
(required) 

This Blog

Syndication

Page view tracker