In this section we are going to talk about LINQ TO SQL. Now, Linq to SQL is a simple Object Relationship Mapper(ORM) which means there is equivalent mapping of your class atrributes with Sql server table columns. This works fine with variety of SQL Server technologies. Here, basically our LINQ queries get translated into SQL queries and do the execution down the layer and fetch or commit the data. Then, what i have done is i have added LINQ TO SQL below in my visual studio to visualize my tables as shown below in the diagrams.
Here, we can also see the relationship between Movie and MovieReviews table by clicking on the arrow and checking the properties there. So, here MovieReviews are associated with Movie via foreign key as shown below in the screen shot.
So, with LINQ to SQL, we have also created the data context with which we are going to interact with SQL.
Now, when i start writing my query on the datacontext, it started giving me properties against which i am going to write my query as shown below in the screen shot. Now, this is the significance of ORM. By default, ORM pluralizes your table, but you can override these properties, but thats ok; this is not in the scope of this topic.
so, here the query is fetching best movies based on the average of rating in descending order.
Now, if i hover on the LINQ expression in debugging window, then you can see that LINQ expression got converted into SQL query and this is how it is going to pick the data from db.
and if i simply copy the expression and put the same in the notepad, i can see that it has created the SQL expression as shown below.
one point to note here that LINQ TO SQL is again lazy loader means, it won’t execute the query in the database until it is invoked from a specific call. In this case it is from foreach() statement. This was how LINQ TO SQL works in a nutshell. Thanks for joining me.
2,453 total views, 1 views today