tags:

views:

109

answers:

3

Hi, im trying to grasp LINQ as I understand it LINQ will be your DAL to your db which in turn creates for you automatically a class that maps out your db structure for you and then you can perform queries using that class ..

  • im trying to do a function that would return the result of my LINQ queries ..
    • all the examples i have been reading seems to point that if I want my queries to be a defined type I have to create a specific class for it and used it as List ..

am I correct in my assumption ?? please do enlighten me

+2  A: 

By default, the Linq to SQL designer will create a class for each table in your database. The database context object that is created will have a definition for each table in your database. You can execute Linq queries against those table definitions and store the results in the generated classes, you have your queries generate anonymous types. It's up to you.

Randy Minder
"You can execute Linq queries against those table definitions and store the results in the generated classes" --> by this do you mean the classes defined with the table structured that has been auto generated in my database context ?
Dennis
The classes L2S generates are not stored with the data context. They are all stored in a single .cs file. The name of the file is the name of your DBML + ".designer.cs".
Randy Minder
+6  A: 

"Linq" is not an ORM technology, it is simply a way to write query like statements inside of .Net languages. You have not said whether you are using Ling to SQL or Linq to Entities, which are both ORM technolgies that work with Linq to map your data schema to tables.

Linq to SQL will provide you with a straight mapping of every table to a class, and will handle most of the lifting to get this done. Your queries can return these types, or using your Linq expression you can "project" these types to other types that you have made yourself.

If you need more flexibility, Entity Framework will allow you to map a single table to multiple entities, multiple tables to a single entity, etc. To do this you have to do a lot more work on your own, since you have to modify the mapping by hand in many cases. Similar to Linq to SQL, queries can return either these generated types or specify their own types in query.

Chris
I'm trying to create a function to run a linq query and return the result .. -- what I want is .. return all the records from table " NDT_Equipment " public List<NDT_Equipment> GetALLRecords() { using (NDT_DB) { return (from a in NDT_DB.NDTEquipment select a); } } - where :NDT_DB - class instance of the database context (autogenerated ) : NDT_Equipment - partial class inside the database context class - i can't compile this .. im getting "are you missing a cast " error
Dennis
@Dennis, please edit your post to add this, and also what ORM are you using?
Chris
- sorry for the post .. i not familiar how to add a comment with paragraphs :(
Dennis
public List<NDT_Equipment> GetALLRecords() { using (NDT_DB) { return (from a in NDT_DB.NDTEquipment select a); } }- using DBLINQ ... - can't get this to compile ..im getting "are you missing a cast"
Dennis
public List<NDT_Equipment> GetALLRecods(){ using (NDT_DB) { return (from a in NDT_DB.NDT_Equipments select a) }} - cant get this to compile .. getting " are you missing a cast " - NDT_DB is the autogenerated class - NDT_Equipment is a partial class inside the NDT_DB}
Dennis
I was suggesting you edit your original post to add this additional part of the problem. You will be able to format it better there, and everyone can see it. Anyways, it looks like you are trying to return a query as a List. You need to call "ToList()" on the query first.
Chris
+1  A: 

Based on the title of your question, i would say no. There could be instances that you have tables that you don't need to map to Linq2SQL. One immediate example that comes to mind, is if you were using the ASP.NET Membership API, you wouldn't need to drag those tables/sprocs/functions into the designer since that data is exposed by the API.

If you are using the designer(DBML file), you can pick and choose which db objects to bring into your solution. This is the way that I have done it, only bringing in those db objects that I actually need access to. As another example, I worked at a shop once that were strongly wed to the idea of all db access being done via stored procedures. So in that case, I worked with a lot of [StoredProcedureName]Results which I in turn mapped to other objects, never the actual tables themselves, which werent even defined in the solution.

Jonathan Bates