Wanted: minimal Northwind LINQ example

  • Thread starter Siegfried Heintze
  • Start date
S

Siegfried Heintze

I'm trying to follow Luca Bolognese's example from
http://www.microsoft.com/emea/msdn/spotlight/sessionh.aspx?videoid=716 but
the following code works for him but not me. VS2008 says there is no
System.Data.Linq!

I tried live searching for a simple example that worked but could not find
one. Can someone tell me how to make Luca's example work or point me to an
example that does work?

Thanks,
siegfried

using System;
using System.Linq;
using System.Data.Linq;
using System.Data.Linq.Mapping;
using System.Collections.Generic;
using System.Text;
 
M

Marc Gravell

In the new project window, ensure that the framework version (top-
right in the new project dialog) is 3.5; for existing (upgraded)
projects, see the project properties dialog - Target Framework is on
the Application tab.

Also ensure that you have references to System.Core.dll and
System.Data.Linq.dll, although simply adding a new "LINQ to SQL
classes" item should be enough to fix this for you.

Marc
 
S

Siegfried Heintze

Thanks marc, that helped.

Now how does intellesense know there is a table customers in the Northwind
database? I'm getting this error for the "var query = db.Customers;" line.
Thanks,
Siegfried

Error 1 'BasicLINQDemo.Northwind' does not contain a definition for
'Customers' and no extension method 'Customers' accepting a first argument of
type 'BasicLINQDemo.Northwind' could be found (are you missing a using
directive or an assembly reference?) C:\Documents and Settings\a-siehei\My
Documents\Visual Studio
2008\Projects\ConsoleApplication1\BasicLINQDemo\Program.cs 38 32 BasicLINQDemo



using System;
using System.Linq;
using System.Data.Linq;
using System.Data.Linq.Mapping;
using System.Collections.Generic;
using System.Text;

namespace BasicLINQDemo
{
[Table(Name="Customers")]
class Customer
{
[Column(IsPrimaryKey=true)]
public string CustomerID { get; set; }
[Column]
public string ContactName { get; set; }
[Column]
public string City { get; set; }
}
class Northwind : DataContext
{
public Table<Customer> customers;
public Northwind(String s) : base(s) { }

}
class Program
{
[System.Runtime.InteropServices.DllImport("msvcrt.dll", SetLastError
= true)]
static extern int _getch();
static System.IO.TextWriter outp = System.Console.Out;
static System.IO.TextReader inp = System.Console.In;
static void Main(string[] args)
{
try
{
outp.WriteLine("start BasicLINQDemo");
Northwind db = new Northwind(".//SQLEXPRESS");
var query = db.Customers;
foreach (Customer c in query)
outp.WriteLine(c.ContactName + ", " + c.City);
}
finally
{
#if noprompt
outp.WriteLine("terminating props.cs");
#else
outp.Write("Enter any key to exit BasicLINQDemo.cs: ");
_getch();
#endif
}
}
}
}
 
J

Jon Skeet [C# MVP]

Siegfried Heintze said:
Now how does intellesense know there is a table customers in the Northwind
database? I'm getting this error for the "var query = db.Customers;" line.

That's because your public field is called "customers" not "Customers".

<snip>
 
S

Siegfried Heintze

boy do I feel stupid. I guess I was trying to do too many things at once.

Anyway, it is still not working. I think it is because I don't have the
northwind example loaded. Why don't I have northwind or pubs databases in my
SQLServer instance?

Thanks,
Siegfried
 
S

Siegfried Heintze

It works in Visual Studio 9! How do I write a script to compile and run this
program? See the embedded comments for my attempt.
Thanks,
Siegfried

Error:
Program.cs(2,14): error CS0234: The type or namespace name 'Linq' does not
exist in the namespace 'System' (are you missing an assembly reference?)

Source:
using System;
using System.Linq;
using System.Data.Linq;
using System.Data.Linq.Mapping;
using System.Collections.Generic;
using System.Text;
/**
* Begin commands to execute this file using MS.NET with CMD.EXE
* csc /out:program.exe /checked /d:noprompt /debug /r:"c:\Program Files
(x86)\Reference Assemblies\Microsoft\Framework\v3.5\System.Data.Linq.dll"
Program.cs
* Program
* del Program.exe
* del Program.pdb
* End commands to execute this file using MS.NET with CMD.EXE
*/

namespace BasicLINQDemo
{
[Table(Name="Customers")]
class Customer
{
[Column(IsPrimaryKey=true)]
public string CustomerID { get; set; }
[Column]
public string ContactName { get; set; }
[Column]
public string City { get; set; }
}
class Northwind : DataContext
{
public Table<Customer> customers;
public Northwind(String s) : base(s) { }

}
class Program
{
[System.Runtime.InteropServices.DllImport("msvcrt.dll", SetLastError
= true)]
static extern int _getch();
static System.IO.TextWriter outp = System.Console.Out;
static System.IO.TextReader inp = System.Console.In;
static void Main(string[] args)
{
try
{
outp.WriteLine("start BasicLINQDemo");
Northwind db = new Northwind(".");
var query = db.customers;
foreach (Customer c in query)
outp.WriteLine(c.ContactName + ", " + c.City);
}
finally
{
#if noprompt
outp.WriteLine("terminating props.cs");
#else
outp.Write("Enter any key to exit BasicLINQDemo.cs: ");
_getch();
#endif
}
}
}
}
 

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