views:

30

answers:

1

I have a lot of pre-linq C# 1.0 code which converts the output from a stored procedure (SQL 2005) into a collection of objects. It goes as follows:

  1. Declare a class with properties matching the columns in the output.
  2. using SqlDataReader, loop while there are rows to read
  3. For every row, add an object to a List
  4. return that list

Although most of that code was generated, I think linq should allow for a smarter way to do it. Do I have to explicitly declare a class? In C# 3.0, how should I converts the output from a stored procedure into a collection of objects?

Ideally I would like to generate code, because I don't like wizards. Thanks!

Edit: I dont have a dbml file. Do I have to have it if I use linq to sql? If yes, how to create it?

+1  A: 

If you are keeping the stored procedures and not doing it writing LINQ to SQL queries, you can just drag the stored procedures onto your dbml file and it will create the classes for you. You can do it two ways if I remember correctly.

1) You can have the return result map to a view.

2) You can have it autogenerate a class for you.

If you stored procedure is something like GetCustomers then it would create a class named something like GetCustomersResult or something like that. If you have a view named uvCustomers then you could tell it to use that view instead of having a different class for every stored procedure that returns the same columns.

Dismissile
Thanks for replying. I dont have a dbml file. Do I have to have it if I use linq to sql. If yes, how to create it?
SQL Cowboy
Usually when you add LINQ to SQL to your project you go to Project -> Add New Item -> Data -> LINQ to SQL Classes. This will create a dbml file for you and you can drag and drop your tables/views/stored procedures from your database to autogenerate your entities.
Dismissile