Part 1 What is LINQ

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
this is part one of linked tutorial in this video we'll discuss what's link benefits of using linked linked architecture and different link providers that are available so what's the link link stands for language integrated query link enables us to query any type of data store be it sequel server XML documents objects in memory like list of customer list of order arrays etc be it entities or be it datasets why should we use link what are the benefits of using link to understand this let's say we are developing a dotnet application and this dotnet application requires to fetch data from different data sources like databases XML documents and from n memory objects like list of customer list of order arrays etc now for a developer who is working on this application for him to be able to fetch data from these different data sources he has to understand the technologies and the syntax that is specific to the underlying data source for example to be able to retrieve data from a sequel server database he needs to understand ad or dotnet and transact sequel that is specific to sequel server database similarly to be able to retrieve data from an XML document the developer has to understand XPath and XSLT and to be able to fetch data from n memory objects the developer has to know how arrays and generics work length enables us to work with these different data sources using a similar programming style without having the need to know the syntax and the technologies that are specific to the underlying data source and another benefit of using Lync is that it provides intelligence and compile time error checking as well well this is an example of that in just a bit but before that let's look at the architecture of link now we all know that we can develop a dotnet application in any dotnet supported programming language like C sharp VB or J sharp similarly a link query can also be written using any of this dotnet supported programming line now between the actual link query and the actual underlying data source there is another component called link provider and it is the responsibility of the link provider to convert that link query into your format that the underlying data source can understand let's say the application is trying to fetch data from sequel server database if that's the case this link query will be fed into a link provider called link to sequel which is going to convert that link query into transact sequel that the underlying sequel server database can understand similarly if the link where e has to fetch data from an XML document you know the same linked query is going to be fed into this link to XML provider which is going to convert that link query into X part which the underlying XML document can understand similarly we have different link providers like link to objects linked to entities linked to data set we even have third party link providers like for example link to Twitter so if we want to query Twitter we can use link to Twitter similarly we have link to Amazon link to Google etc now let's look at an example of using link but before that let's actually write some ad or dotnet code and now see what are the problems with that and then we'll try and rewrite the same example using length so let's flip to sequencing or management studio so this is the table that we'll be using for the demo so here we have both male and female students all we want to do is write area dot net code to retrieve only male students and then display them within the grid view control and to achieve that I have already created an MT asp.net web application project here and all I have done so far is within the web config file I have a connection string here which points to this sample database and I also have a webform one here where I have dragged and dropped a grid we control and within the code-behind file I have brought in the required Adria dotnet namespaces and here you see the regular ad or net code now I'm not going to discuss in all the specific details of radio dotnet here because we have already discussed that in ATO dotnet video series so if you're new to a do dotnet please watch videos from ato dotnet video tutorial so what are we doing here we are reading the connection string from web config file and using that connection string we are building the sequel connection object and then here we have the actual sequel command that we want to be executed against sequel server so a do dotnet and this is the transact sequel that is specific to sequel server database look at the query itself the query is straightforward we are saying select all these columns from students table where it gender equals male now this query is buried inside this opaque string which means here notice that we don't have any form of intelligence and since we don't have intellisense here you know this query is error-prone meaning we can make some spelling mistakes for example you know instead of first-name we could very easily misspell it it's a first name one and you know that's the sequel query right and then here we're constructing list of students you know and here we have the actual student class so this student class is straightforward with ID first name last name and gender properties okay now so we are constructing list of students here open the connection executing the command and then we are looping through the reader object and while we are looking through your creating an instance of student and then we are retrieving data from the reader and populating the respective properties of the student object adding the student of jekt to the list closing the connection and then we are setting that list of students as the data source for the grid view control and invoking data bind so let's quickly run this and see if it so it should be able to fetch only the male students and that's what we see here now since we don't have intellisense inside this opaque string you know it's very easy to make a spelling mistake like that and if we do that and look at this when we build a solution we notice the status bar here we don't have any compilation error okay it says pill succeeded but when we actually run this at run time notice that we are going to have that error so you know regular ad or dotnet since the queries are buried inside opaque strings we don't have intelligence or we don't have compile time error checking with link we get that as well now let's see how to rewrite the same example using link okay for that let's actually delete this wave from one dot aspx and let's add an over from one again and let's set font family here to area and let's drag and drop a Calitri control okay now let's get to the code-behind file now we need to add another component here that's called linked to sequel classes will discuss link to sequel in detail in a later video session but for now let's go ahead and add a new item and click on the data tab under install the template select link to sequel classes and let's call this sample dot DB ml and click Add ok so now let's open the silver Explorer so using this we are going to connect to actual sequel server database from Visual Studio and right click on data connections and select this option add connection so here specify your server name I'm working with you know local installation of sequel server here so I can specify dot and then here we should be able to see the list of databases that are available or you can also give the name of the server here the name of sequel server on my machine it is meant at - PC and I'm going to use Windows authentication to connect to that sequel server and we want to work with sample database ok so that should add a connection to sequel server from visual studio and then once we expand tables we should be able to see the students table so now we are going to drag and drop the student table onto this designer surface which is going to generate you know student class for us ok so within the sample dot designer dot CS file we should see a class for student now we are not going to get into the specifics of link to sequel here we'll discuss that in detail in a later video session so all we have done so far is generated a class for student table ok and then within our code behind file we are going to write a link very now another thing that we need to understand here is in addition to the actual student class we also have something called sample data context here so this is the gateway into the database so we have to create an instance of the sample data context class in order to be able to retrieve data from the sample database okay so the first thing that we need to do within the code-behind file is to create an instance of that class sample data context let's call this data context equals new sample data context now what we want to do is retrieve data from sequel server database using a link query okay so and whatever that link query is going to return we are going to set that as the data source for the gridview control okay so with the link query we first write the from keyword so from okay and then we specify name of a variable here let's call it student okay n now we specify our actual source where are the students present now students are present in this data context so data context dot students okay and now we don't want all the students if we want all the students we can simply say select student okay and then when we say grid v1 dot data bind so when we run this we should get all the students because we are not filtering them so we have all the male and female students okay now if we want to filter them we use a ver keyword so where student dot gender equals we only want male employees okay so this query this link very right here should return male employees that are present in students table so let's quickly learn this and see if that's what is the output that we get so notice that we only get male students and again notice that here we have Intel cents as well the moment I say student dot it shows all the properties of the student object okay now if I misspell for example gender to gender one and then if I try to build the solution notice that we'll get to know about that at the compile time itself so we have you know intellisense and compile time error checking okay but with ATO dotnet we don't have that because the queries are buried inside that opaque strings we don't have intelligence and as a result of that we don't have compile time error checking as well alright and another thing to notice is so this is a link very now we want this query to be able to retrieve data from underlying sequel server database but can sequel server understand this link very now sequel server can only understand transact sequel so there has to be someone in between who is going to convert this link query into transact sequel that the underlying database can understand and who is that going to be that is what is the link providers job so link to sequel is going to take that link very convert that into transact sequel and send it to sequel server database and to prove that you can actually fire up sequel profile ax and then run the application and see the corresponding transact sequel query that is generated so let's run a trace and then let's go ahead and run this so now when we go to sequel profile ax stop the trace notice that this is what is they transact sequel that is generated so here we have some dynamic sequel that is generated by link to sequel provider so when we execute this query we should only get male employees right so we have looked at the same example okay now you know we have seen how to write a linked query to work with you know databases now let's say for example you know I want to work with in memory objects like an array for example let's say I have an integer array here so in detail array let's call it numbers equals let's say one to ten numbers and to just speed things up I have already typed those numbers so let's copy and paste it right there so we have numbers from 1 to 10 now out of these numbers I want to retrieve even numbers and then display them within the grid view control ok again to achieve that we can use link query ok so let's comment this so this one right here is linked to see file because this is going to work with link to sequel provider and then convert that into a transaction now let's see how to work with objects in memory using link very so the same idea accurate v1 dot data source is going to be so we first write the from keyword and let's give this analyze from number n so what is the data source here numbers you know the array that is an in-memory object now what we want we want only in our even numbers so I'm going to say we're number mod 2 equals 0 so if we divide a number by 2 and of the reminder is 0 then we know for sure it's an even number so that's what is the condition that we are specifying next to the vert operator and then what we want to do if that is true then we want to select such kind of a number and this is you know this logic is going to be applied on every number within this object numbers which is an array of numbers okay and then simply invoke data bind so now when we run this we should actually say only the even numbers within the grid we control 2 4 6 8 10 now look at this within the example that we have looked at so far bit worked with different data sources one data source here is transact sequel server database and the other data source is in memory objects but the LINQ query is pretty much similar locate depending on the data source it's not going to change that much it is the actual sequel provider that is going to convert that link query into a syntax that is specific to the underlying data source that's it for today thank you for listening have a great day
Info
Channel: kudvenkat
Views: 554,487
Rating: undefined out of 5
Keywords: linq tutorial, linq tutorial c#, asp.net, .net, dot net, beginners, playlist, kudvenkat
Id: z3PowDJKOSA
Channel Id: undefined
Length: 17min 2sec (1022 seconds)
Published: Sat Jun 28 2014
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.