Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / March 2008

Tip: Looking for answers? Try searching our database.

Wanted: minimal Northwind LINQ example

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Siegfried Heintze - 06 Mar 2008 01:34 GMT
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;
Marc Gravell - 06 Mar 2008 04:51 GMT
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
Siegfried Heintze - 07 Mar 2008 22:30 GMT
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
           }
       }
   }
}
Jon Skeet [C# MVP] - 07 Mar 2008 22:36 GMT
> 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>

Signature

Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk

Siegfried Heintze - 07 Mar 2008 23:24 GMT
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
Jon Skeet [C# MVP] - 08 Mar 2008 07:43 GMT
> 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?

I don't believe it comes with SQL Server 2005 Express. You can get it
here though:

http://blogs.msdn.com/smartclientdata/archive/2005/11/02/488258.aspx

Signature

Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk

Siegfried Heintze - 11 Mar 2008 00:52 GMT
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
           }
       }
   }
}

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.