LINQ Newbie

Z

zacks

I am trying to get into LINQ. I have created ConsoleApplication
project and have pasted in the code from one of the examples in help:

DataContext db = new DataContext(@"c:\program files
\microsoft sql server\mssql\data\northwnd.mdf");
Table<Customer> Customers = db.GetTable<Customer>();
var query = from cust in Customers where cust.City ==
"London" select cust;
foreach (var cust in query)
Console.WriteLine(string.Format("id = {0}, City =
{1}", cust.CustomerID, cust.City));

But the compiler barfs on the second line. Apparently, the Customer
object is a "typed table". The example does not tell you how to create
this typed table.

I have searched help and cannot find any direction. Can someone point
me in the right direction?
 
M

Marc Gravell

Well, Table<Customer> could be (I guess) described as a "typed
table"... what is the *exact* error message?

Marc
 
Z

zacks

Well, Table<Customer> could be (I guess) described as a "typed
table"... what is the *exact* error message?

Marc

I called it a typed table because the example I pulled that code from
had a comment before thedeclaration of the Customers table object that
said:

// Get a typed table to run queries.

The build error I get is:

The type or namespace name "Customer" could not be found (are you
missing a using directive or an assembly reference?)

Actually, I get two of those for the two references to Customer in the
statement.

I am also wondering how this works since the actual table in the
database is named Customers, not Customer.
 
M

Marc Gravell

Well, you still need to define the Customer type; if you use the IDE
tools, this is drag'n'drop operation, but you can write LINQ-to-SQL
classes by hand too. Based on the "DataContext" (rather than
"SomeSpecificDataContext") I'd guess the example is following the
hand-crafted approach, so you definitely need to declare a Customer type
along with all the necessary metadata.

Re the type being Customer and the table Customers - this isn't a
problem; you can tell LINQ-to-SQL about this mapping in either the
attributes that are attached to the type (TableAttribute etc) and its
members (ColumnAttribute etc), or via a separate xml file. If you are
using the IDE tools, then you just tweak the properties in the designer
(which writes the "dbml" file), and it writes the suitable attributes
for you.

Marc
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top