LINQ Newbie

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • zacks@construction-imaging.com

    LINQ Newbie

    I am trying to get into LINQ. I have created ConsoleApplicat ion
    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\da ta\northwnd.mdf ");
    Table<CustomerC ustomers = db.GetTable<Cus tomer>();
    var query = from cust in Customers where cust.City ==
    "London" select cust;
    foreach (var cust in query)
    Console.WriteLi ne(string.Forma t("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?

  • Marc Gravell

    #2
    Re: LINQ Newbie

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

    Marc

    Comment

    • zacks@construction-imaging.com

      #3
      Re: LINQ Newbie

      On Jun 19, 4:50 pm, Marc Gravell <marc.grav...@g mail.comwrote:
      Well, Table<Customerc ould 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.

      Comment

      • Marc Gravell

        #4
        Re: LINQ Newbie

        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 "DataContex t" (rather than
        "SomeSpecificDa taContext") 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 (ColumnAttribut e 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

        Comment

        Working...