ServicesResourcesConferencesOur TeamWeblogsAboutContact
   

Subscriptions

Post Categories

News

My new books

My Blogroll

INETA UG Leaders

Affiliations

News

Archives

Christian Nagel's OneNotes

.NET Training, Consulting, Coaching & Development


LINQ Part 2 - Filtering & Sorting Using Data from the Database

In the first part of this LINQ series I've shown how to filter and sort an object list:

var winners = from r in racers
   where r.Wins > 3
   orderby r.Wins descending
   select r;

foreach (Racer r in winners)
{
   Console.WriteLine("{0}, {1}", r.Name, r.Wins);
}

Now I'm showing the same result from racers stored inside the database. Racers should be filtered according to the number of race wins, and sorted.

The traditional way is by creating a connection using an ADO.NET connection class, defining the SELECT statement using the SqlCommand class and defining a SELECT statement including WHERE and ORDER BY. With a data reader all records that are already sorted can be read.

SqlConnection connection = new SqlConnection(@"server=localhost\yukon;database=Formula1;trusted_connection=true");
SqlCommand command = connection.CreateCommand();
command.CommandText = "SELECT Name, Wins From Racers WHERE Wins > 3 ORDER BY Wins DESC";
connection.Open();
SqlDataReader reader = command.ExecuteReader(CommandBehavior.CloseConnection);
while (reader.Read())
{
   Console.WriteLine("{0}, {1}", 
      reader.GetString(0),
      reader.IsDBNull(1) ? 0 : reader.GetInt32(1));
}
reader.Close();

How can the same be achieved usind DLINQ?

Formula1Database db = new Formula1Database(@"server=localhost\yukon;database=Formula1;trusted_connection=true");

var winners = from r in db.Racers
      where r.Wins > 3
      orderby r.Wins descending
      select r;

foreach (Racer r in winners)
{
      Console.WriteLine("{0} {1}", r.Name, r.Wins);
}

Compare the code accessing the database with the code from the first part of the article accessing an object list. The same syntax is used accessing object lists and data from the database.

To get the data from the database, the Formula1Database class derives from the base class DataContext. With the constructor a connection string is passed to the base class. The class also contains a field that maps to the Racers table in the database returning Racer objects.

    public class Formula1Database : DataContext
    {
        public Formula1Database(string fileOrServerOrConnection)
            : base(fileOrServerOrConnection)
        {
        }

        public Table<Racer> Racers = base.GetTable<Racer>();

    }

The mapping to the Racers table in the database is defined with [Table] and [Column] attributes:

   [Table(Name="Racers")]
    public class Racer : IComparable<Racer>
    {
        //...

        [Column(Id=true)]
        private int id;

        private string name;

        [Column]
        public string Name
        {
            get { return name; }
            set { name = value; }
        }

        private string car;

        [Column]
        public string Car
        {
            get { return car; }
            set { car = value; }
        }

        private int wins;

        [Column]
        public int Wins
        {
            get { return wins; }
            set { wins = value; }
        }

        //...

Part 3 will show how filtering and sorting can be done using XML data.

Christian

posted on Monday, March 13, 2006 5:28 PM

# LINQ Part 3 - Filtering and Sorting XML @ Friday, March 17, 2006 1:33 PM

LINQ part 3 shows filtering and sorting XML data using XLINQ and XSLT.
Christian Nagel's OneNotes

# LINQ Part 3 - Filtering and Sorting XML @ Friday, March 17, 2006 5:17 PM

LINQ part 3 shows filtering and sorting XML data using XLINQ and XSLT.
Christian Nagel's OneNotes

# Link Listing - March 19, 2006 @ Monday, March 20, 2006 4:13 AM

CAB Best Practices
[Via: ]
How to detect VS.NET DesignMode in server
controls [Via: Rumen Stankov...
Christopher Steen

# LINQ Part 4 - What's behind a query expression? @ Thursday, March 30, 2006 4:36 PM

LINQ part 4 shows what's behind query and lambda expressions.
Christian Nagel's OneNotes

# LINQ Part 4 - What's behind a query expression? @ Friday, March 31, 2006 12:24 AM

LINQ part 4 shows what's behind query and lambda expressions.
Christian Nagel's OneNotes

# Language-Integrated Query (LINQ) @ Saturday, April 08, 2006 7:17 PM

I ran across four very interesting links (no pun intended) related to&amp;nbsp;Language-Integrated Query&amp;nbsp;(LINQ)...
Michael Primeaux's Blog

# Language-Integrated Query (LINQ) @ Saturday, April 08, 2006 9:56 PM

I ran across four very interesting links (no pun intended) related to&amp;nbsp;Language-Integrated Query&amp;nbsp;(LINQ)...
Michael Primeaux's Blog

# Language-Integrated Query (LINQ) @ Sunday, April 09, 2006 3:34 AM

I ran across four very interesting links (no pun intended) related to&amp;nbsp;Language-Integrated Query&amp;nbsp;(LINQ)...
Michael Primeaux's Blog

# Language-Integrated Query (LINQ) @ Sunday, April 09, 2006 3:34 AM

I ran across four very interesting links (no pun intended) related to&amp;nbsp;Language-Integrated Query&amp;nbsp;(LINQ)...
Michael Primeaux's Blog

# More LINQ @ Monday, May 15, 2006 10:01 AM

Scott Guthrie has some great information on using LINQ with ASP.NET.
Christian Nagel's OneNotes


Powered by Community Server, by Telligent Systems