Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Linq - how does it work?

I have just been looking into Linq with ASP.Net. It is very neat indeed. I was just wondering - how do all the classes get populated? I mean in ASP.Net, suppose you have a Linq file called Catalogue, and you then use a For loop to loop through Catalogue.Products and print each Product name. How do the details get stored? Does it just go through the Products table on page load and create another instance of class Product for each row, effectively copying an entire table into an array of class Product?

If so, I think I have created a system very much like this, in the sense that there is a SiteContent module with an instance of each Manager class - for example there is UserManager, ProductManager, SettingManager and alike. UserManager contains an instance of the User class for each row in the Users table. They also contain methods such as Create, Update and Remove. These Managers and their "Items" are created on every page load. This just makes it nice and easy to access users, products, settings etc in every page as far as I, the developer, am concerned. Any any subsequent pages I need to create, I just need to reference SiteContent.UserManager to access a list of users, rather than executing a query from within that page (ie this method separates out data access from the workings of the page, in the same way as using code behind separates out the workings of the page from how the page is layed out).

However the problem is that this technique seems rather slow. I mean it is effectively creating a database on every page load, taking data from another database. I have taken measures such as preventing, for example, the ProductManager from being created if it is not referenced on page load. Therefore it does not load data into storage when it is not needed.

My question is basically whether my technique does the exact same thing as Linq, in the sense of duplicating data from tables into properties of classes..

Thanks in advance for any advice or answers about this.

Regards,

Richard Clarke

like image 810
ClarkeyBoy Avatar asked Mar 19 '10 20:03

ClarkeyBoy


2 Answers

Linq to SQL does not maintain copies of all the data in the database. It takes the expressions you feed it through the IQueryable interfaces, reads the expression trees, and converts them to actual SQL statements using WHERE and other SQL constructs.

In other words, when you write this:

var product = context.Products.Where(p => p.ID == 50).SingleOrDefault();

It executes this against the database:

SELECT ID, Name, Foo, Bar, Baz, Blah, ...
FROM Products
WHERE ProductID = 50

It does not just do a SELECT * FROM Products and then search for the specific product from within the results.

Your application will be slow indeed if you try to "download" the entire database on every page load. That is definitely not what Linq to SQL, Linq to Entities, or any other ORM framework does.

like image 198
Aaronaught Avatar answered Oct 18 '22 17:10

Aaronaught


Linq-to-SQL definitely does not "create a database on every page load"..... you need to get your head around how Linq-to-SQL work, and what it does.

My recommendation: check out the multi-part series of blog post by "The Gu" - Scott Guthrie - and soak up all those perls of wisdom in those posts.

A lot of the magic is hidden behind the visual designer, which basically creates a "snapshot" of the table structures - but only of the structures! And it knows how to convert your "gimme those records" queries into straight T-SQL queries against the SQL Server backend. And it converts those relational bits and pieces into nice .NET objects.

Sufficiently advanced technology is always almost indistinguishable from magic - but Linq and Linq-to-SQL really aren't black magic - just very clever pieces of code by some bright guys!

like image 20
marc_s Avatar answered Oct 18 '22 16:10

marc_s