none
LINQ to SQL-Northwind Windows Forms application in VC# 2008 Express: 'System.Linq.IQueryable<ORDesignerWalkthrough.Form1.Customer>' does not contain a definition for 'Customer' and no extension method 'Customer' accepting a first argument of type RRS feed

  • Question

  • Hi all,

    In my Visual C# 2008 Express, I created a LINQtoSqlWindowsForm-Northwind-ORmapping project with the following code:

    //Walkthrough:Creating LINQ to SQL Classes (O/R Designer (bb384428.aspx)
    //LINQ to SQL: .NET Language-Integrated Query for Relational Data by 
    //Dinesh Kulkarni, et. al, March 2007 (bb425822.aspx)
    // I modified code on 05 Oct 2009 
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.Data.Linq;
    using System.Data.Linq.Mapping;
    
    namespace ORDesignerWalkthrough
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
            [Table(Name = "Customers")]
            public class Customer
            {
                [Column(IsPrimaryKey=true)]
                public string CustomerID;
                [Column]
                public string City;
            }
    
    
    
            private void Form1_Load(object sender, EventArgs e)
            {
                // DataContext takes a connection string
                DataClasses1DataContext db = new DataClasses1DataContext(); //"c:\\LinqToSQLNorthwindApp-JohnChen\\NORTHWIND.MDF");
    
                // Get a typed table to run queries
                Table<Customer> Customers = db.GetTable<Customer>();
    
                // Query for customers from London
                var q =
                    from c in Customers
                    where c.City == "London"
                    select c;
                foreach (var cust in q)
                {
                    //  Try to see if we get any data here
                    MessageBox.Show("We got data");
                    Customer c = q as Customer;
                    MessageBox.Show(q.Customer);
                }
                // This is my way to do the databinging
                this.dataGridView1.DataSource = Customers;
            }
        }
    }
    I got the following compiler error:

    Error 1 'System.Linq.IQueryable<ORDesignerWalkthrough.Form1.Customer>' does not contain a definition for 'Customer' and no extension method 'Customer' accepting a first argument of type 'System.Linq.IQueryable<ORDesignerWalkthrough.Form1.Customer>' could be found (are you missing a using directive or an assembly reference?) C:\Documents and Settings\e1enxshc\My Documents\Visual Studio 2008\Projects\ORDesignerWalkthrough\ORDesignerWalkthrough\Form1.cs 53 35 ORDesignerWalkthrough

    I do not know how to fix it.  Please help and advise me where I made the mistake and how to correct the problem.

    Thanks in advance,
    Scott Chang
    SHC
    Monday, October 5, 2009 8:06 PM

Answers

  • Hi Scott,

    I see that in the foreach loop, you are trying to cast the list of customers (IQueryable<Customer>) to a single customer. Try this instead

                //for each customer record found in London    
                foreach (var cust in q)
                {
                    //  Try to see if we get any data here
                    MessageBox.Show("We got data");
    
                    //print the ID of the current customer
                    MessageBox.Show(cust.CustomerID);
                }
    
    Also, I noticed that you are assigning the whole customers table as the datasource for your datagrid. Should it not be the customers from London only?

    this.dataGridView1.DataSource = q

    Hope that makes sense.

    Regards,

    Syed Mehroz Alam
    My Blog | My Articles
    • Marked as answer by Scott Chang Tuesday, October 6, 2009 3:29 PM
    Tuesday, October 6, 2009 4:34 AM
  • Syed's answer is correct.
    John Chen -- See my team blog: http://blogs.msdn.com/vsdata. All my posts are provided "AS IS" with no warranties, and confer no rights.
    • Marked as answer by Scott Chang Tuesday, October 6, 2009 3:29 PM
    Tuesday, October 6, 2009 4:59 AM
  • Hi Scott,

    You can remove the unwanted autogenerated columns by using DataGridView.Columns.Remove(), replace your last statement with this:

    // This is my way to do the databinding 
    this.dataGridView1.AutoGenerateColumns = true;
    this.dataGridView1.DataSource = custs;  
    
    //write the following statement for each column you want to remove
    this.dataGridView1.Columns.Remove("MyUnwantedColumn1");
    this.dataGridView1.Columns.Remove("MyUnwantedColumn2");
    this.dataGridView1.Columns.Remove("MyUnwantedColumn3");


    Regards,
    Mehroz
    Syed Mehroz Alam
    My Blog | My Articles
    • Marked as answer by Scott Chang Tuesday, October 6, 2009 6:51 PM
    Tuesday, October 6, 2009 5:43 PM

All replies

  • Hi Scott,

    I see that in the foreach loop, you are trying to cast the list of customers (IQueryable<Customer>) to a single customer. Try this instead

                //for each customer record found in London    
                foreach (var cust in q)
                {
                    //  Try to see if we get any data here
                    MessageBox.Show("We got data");
    
                    //print the ID of the current customer
                    MessageBox.Show(cust.CustomerID);
                }
    
    Also, I noticed that you are assigning the whole customers table as the datasource for your datagrid. Should it not be the customers from London only?

    this.dataGridView1.DataSource = q

    Hope that makes sense.

    Regards,

    Syed Mehroz Alam
    My Blog | My Articles
    • Marked as answer by Scott Chang Tuesday, October 6, 2009 3:29 PM
    Tuesday, October 6, 2009 4:34 AM
  • Syed's answer is correct.
    John Chen -- See my team blog: http://blogs.msdn.com/vsdata. All my posts are provided "AS IS" with no warranties, and confer no rights.
    • Marked as answer by Scott Chang Tuesday, October 6, 2009 3:29 PM
    Tuesday, October 6, 2009 4:59 AM
  • Hi Syed and John,  Thanks both for your nice responses.

    To Syed:  (i) The following code worked: 
                //for each customer record found in London   
                foreach (var cust in q)
                {
                    //  Try to see if we get any data here
                    MessageBox.Show("We got data");

                    //print the ID of the current customer
                    MessageBox.Show(cust.CustomerID);
                }
                      I got the right  "We got data" MessageBox and CustomerID 6 times as expected.
                    (ii) The following code statment did not work for me:
                       
    this.dataGridView1.DataSource = q
             I got an empty DataGridView1!!!???  I added one column "CustomerID" to DataGridView1 and then executed the project again. It did not work either.   What is wrong with this DataGridView1 output?

    To John:  I tried the old code statement "this.dataGridView1.DataSource = Customers;" in this project also.  This did not
                  work either!!!???   What did I miss in setting the properties of DataGridView1 for getting the output of the 6
                  "London-related" CustomerIDs?

    Please help and advise me again.

    Thanks again,
    Scott Chang
    SHC
    Tuesday, October 6, 2009 1:39 PM
  • Hi Syed and John,  Please disregard my last post, because I figured out why I did not get the output of 6-"London-related"-CustomerID DataGridView:  I added {get; set;} to the 2 code statements  "public string CustomerID" and "public string City" in the  "public class Customers" section:

    [

    Table(Name = "Customers")]

     

    public class Customer

    {

    [

    Column(IsPrimaryKey=true)]

     

    public string CustomerID {get; set;}

    [

    Column]

     

    public string City { get; set; }

    }

    My project "ORDesignerWalkthrough" worked perfectly and it generated the 6-"London-related"-CustomerID DataGridView as you instructed.  

    Syed, I used the same technique to do the following code for my project "scWinFrmLinqToSqlNorthwindORMappingClasses:
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Data.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.Data.Linq.Mapping;
    
    namespace scWinFrmLingToSqlNothwindORMappingClasses
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
            [Table(Name = "Customers")]
            public class Customers
            {
                [Column(IsPrimaryKey = true)]
                public string customerId { get; set; }
                [Column]
                public string companyName { get; set; }
                [Column]
                public string city { get; set; }
                [Column]
                public string country { get; set; }
            }
    
            private void Form1_Load(object sender, EventArgs e)
            {
    
                DataClasses1DataContext db = new DataClasses1DataContext();
                Table<Customer> customers = db.GetTable<Customer>();
                var custs = from c in customers select c;
    
                foreach (var x in custs)
                {
                    // Try to see if we get any data here. 
                    // MessageBox.Show("We got data");
                    Customer c = x as Customer;
                    // MessageBox.Show(c.ContactName);
                }
    
                // This is my way to do the databinding 
                this.dataGridView1.DataSource = custs;     
            }
    
    
        }
    }
    

     

    I got the output of DataGridView for all the columns of Customers (not just 4 columns of CustomersID, CompanyName, City, and Country).  Syed, what should I do to get just these right 4 columns printed in the DataGridView output?  Please help and advise again.

    Thanks again,

    Scott Chang    


    SHC
    Tuesday, October 6, 2009 3:29 PM
  • Hi Scott,

    You can remove the unwanted autogenerated columns by using DataGridView.Columns.Remove(), replace your last statement with this:

    // This is my way to do the databinding 
    this.dataGridView1.AutoGenerateColumns = true;
    this.dataGridView1.DataSource = custs;  
    
    //write the following statement for each column you want to remove
    this.dataGridView1.Columns.Remove("MyUnwantedColumn1");
    this.dataGridView1.Columns.Remove("MyUnwantedColumn2");
    this.dataGridView1.Columns.Remove("MyUnwantedColumn3");


    Regards,
    Mehroz
    Syed Mehroz Alam
    My Blog | My Articles
    • Marked as answer by Scott Chang Tuesday, October 6, 2009 6:51 PM
    Tuesday, October 6, 2009 5:43 PM