none
Type or namespace name not found RRS feed

  • Question

  • I am using the sample code in: How to: Connect to a Database (LINQ to SQL)

    I am able to compile without errors except the following compile error:
    error CS0246: The type or namespace name 'Customers' could not be found (are you missing a using directive or an assembly reference?)

    The error is for the line:

    public Table<Customers>;

    I agree with the compiler; I don't see how it knows what the Customers table is. I get that error when I use the sample code that uses the Northwind class but I also get the error if I don't use the class and use code that uses GetTable.

    I apologize for asking such a simple question; I assume there is a simple answer and I need to read all about LINQ and all of that. If someone does not mind answering this question, that will help.


    Sam Hobbs; see my SimpleSamples.Info
    Thursday, November 19, 2009 11:13 PM

Answers



  •                   You are actually trying to access the class version of the table customers when you say Table<Customers>.but you did not create such one  So you must create the class version for the tables before you actually accessing it in the code .


    below code will run to get the customer details from NothWnd database



    using System;
    using System.Linq;
    using System.Data.Linq;
    using System.Data.Linq.Mapping;
    public class LinqDataContext
    {

     public static void Main()
     {

    Northwind db= new Northwind();
     var  q = from c in db.Customers
              where c.City == "London"
              select c;
            foreach(var c in q)
            {
               Console.WriteLine("Id={0}, City={1}",c.CustomerID,c.City);

           }
      
     }
    }

    //Northwind class by Inheriting Data context class

    public partial class Northwind : DataContext
    {
     public Table<Customer> Customers;
     public static string connection = "C:\\Northwnd.mdf";
     public Northwind():base(connection){}

    }

    // this is the class representation of customer table

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

    Hope this helps ..

    • Marked as answer by Simple Samples Friday, November 20, 2009 7:12 AM
    Friday, November 20, 2009 5:32 AM

All replies



  •                   You are actually trying to access the class version of the table customers when you say Table<Customers>.but you did not create such one  So you must create the class version for the tables before you actually accessing it in the code .


    below code will run to get the customer details from NothWnd database



    using System;
    using System.Linq;
    using System.Data.Linq;
    using System.Data.Linq.Mapping;
    public class LinqDataContext
    {

     public static void Main()
     {

    Northwind db= new Northwind();
     var  q = from c in db.Customers
              where c.City == "London"
              select c;
            foreach(var c in q)
            {
               Console.WriteLine("Id={0}, City={1}",c.CustomerID,c.City);

           }
      
     }
    }

    //Northwind class by Inheriting Data context class

    public partial class Northwind : DataContext
    {
     public Table<Customer> Customers;
     public static string connection = "C:\\Northwnd.mdf";
     public Northwind():base(connection){}

    }

    // this is the class representation of customer table

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

    Hope this helps ..

    • Marked as answer by Simple Samples Friday, November 20, 2009 7:12 AM
    Friday, November 20, 2009 5:32 AM
  • Yes, that answers the question; thank you. I think the sample in the MSDN is incomplete; it would help if it provide just the few extra lines of code that you provided in your sample. I had difficulty finding samples that were adequately complete. Obviously I need to get a book about LINQ; the MSDN is not enough.
    Sam Hobbs; see my SimpleSamples.Info
    Friday, November 20, 2009 7:15 AM
  • OK - I know this is reviving an old thread, but I tried this example .. and am having some problems.

    Firstly I'm using Visual C# 2010 Express

    Secondly the following lines are giving me problems.using

    System.Data.Linq;using 
    System.Data.Linq.Mapping;

    .Data.Linq is not an available option .. ???

    Also ... I'm getting the following error messages:

    1. The type or namespace name 'Northwind' could not be found (are you missing a using directive or an assembly reference?)  
    2. The type or namespace name 'Table' could not be found (are you missing a using directive or an assembly reference?)      

    Does anyone have any ideas???

    Thanks - Pavilion

    Thursday, January 30, 2014 2:08 AM