views:

83

answers:

1

I´m (professionally) creating a SQL Server database client by using Visual Studio 2008, C# -> Windows Form(s). And I´m using all the built in stuff, provided by my friend VS Studio, dragging and dropping, creating SQL query tables in DataSet.xsd, and so on... I like that.

But!

I would like to try out LINQ, as I would like to have something that to me is more intuitive than pure SQL...

And (here comes the newbie-problem to be solved)!

I don´t know where to put the LINQ code to make a table "pop up" in the Data Sources window - meaning I´m completely stuck! How should I do it?

A: 

In the VS2008 menu,
Project > Add New Item… > Data (category) > LINQ to SQL Classes

A designer will open up.

Open your database in the Server Explorer pane, and drag you tables onto the design surface.

Edit after comments

Here is an example of using LINQ to query the database in code:

using(var db as NameOfYourLinqDataContextClass)
{
    var myCars = from car in db.cars
                     where car.owner_name.Equals("Jay")
                     select car;

    foreach (var car in myCars)
    {
        myForm.myCarsList.Add(string.Format("{0} {1}", car.make, car.model));
    }
}

Here I've queried the cars table in my database for any row where the value in the car_owner column is "Jay." Using this query, I concatenate the values from the make and model columns and put them in a list or something on my form.

LINQ-to-SQL creates CLR classes based on the schema of your database. You create these query expressions "on-the-fly," which get converted into T-SQL queries behind the scenes and are executed against the database when the results are needed. That is to say that in the example above, no call is made to the database until the line foreach (var car in myCars).

Jay
Let´s see if I´m following you in this "example-scenario"...1. I drag two tables (let´s call them "People" and "Roles") from Data Source pane into the (newly created) DataClasses1.dbml-layout 2. Now, where (in what file / file type) do I put my LINQ code?(I lost you already)3. And if I put some LINQ queries in the right place (see bullet 2), will I then be able to (and here comes my initial question) - make a table "pop up" in the DATA SOURCES pane?As I want to drag/drop some columns from there into a Windows Form.
Kent S. Clarkson
Once you drag in your tables, build the solution. From the VS menu, go to `Data > Add New Data Source…`. Choose "object" type. Navigate the structure of your application to find the objects with the name of your table and click "Finish." Do it again for each table that you want to use as a data source.
Jay
Thanks Jay, I´ll test it out tomorrow - and hopefully your answer also will lead me to understand where to put the LINQ query code in this the described scenario - otherwise I might bug you again. As I of course want the new tables to be awsome query tables (see bullet 2 in my last "post").
Kent S. Clarkson
Yep, now you showed me how to populate the Data Sources pane - though, the unique tables aren´t selectable as Data Sources of Object type, but the whole DataContext in which they initially were dragged. That´s as it should be, right? So thanks for that! If you also can tell me where (in what file / file type) to put the LINQ query code to manipulate those tables before using them as data sources, I will set your answer to accepted.
Kent S. Clarkson
No, you should be able to select the table-mapped objects themselves. Make sure that you build the project before adding the data source. The DataContext won't be a lot of use as a DataSource. I think there may be some confusion as to where LINQ comes into play here -- you don't use it to modify the table itself, rather to create ad hoc queries *in code*. I'll update my answer to show a sample usage.
Jay
OK, nice! So I can put the code in the Form code, for example? And the LINQ code you wrote is compliant with C#? - strange anyway that the table-mapped objects weren´t selectable, as I built the project - I´ll give it a new try when I get some time... that is - not immediately. Thanks for your effort :-)
Kent S. Clarkson
Yeah, you can put the code anywhere that you need to get data from or write data to the database. I recommend that you do a search for LINQ-to-SQL getting started screencasts, blogs, tutorials to learn the syntax. There are also extension methods that do the same thing: `var myCars = db.cars.Where(car => car.owner_name.Equals("Jay"));`
Jay