LINQ – .NET Language-Integrated Query for Relational Data

LINQ is basically a translation layer between the class paradigm for programming languages and rows in a relational database. Instead of the programming languages using SQL to query the database, they use instances of their classes and LINQ together. LINQ does all the SQL interaction.

When I first started reading about it, it reminded me of “Ruby on Rails”, specifically the “Convention over Configuration” paradigm.

For example, look at this C# class:

[Table(Name="Customers")]
public class Customer
{
public string CustomerID;
public string City;
}

And now this quote from the MSDN help: “The Table attribute has a Name property that you can use to specify the exact name of the database table. If no Name property is supplied, LINQ to SQL will assume the database table has the same name as the class.”

Except, there’s one little problem. Does the class really need to have the same name as the Table, or does the class need to have the same name as the Table minus the “s”?

Hopefully, I’ll remember to update this post when I learn the answer.

Oh, but look at this code!

[Table(Name="Customers")]
 public class Customer
 {
 [Column(IsPrimaryKey=true)]
 public string CustomerID;
 [Column]
 public string City;
 }

Now, there’s a “Column” keyword that tags the data member as a column in a database table. According to the same MSDN Help, “As with the Table attribute, you only need to supply information in the Column attribute if it differs from what can be deduced from your field or property declaration.”

This is “Convention over Configuration” at work.

Comments are closed.