Showing posts with label LinQ To Sql. Show all posts
Showing posts with label LinQ To Sql. Show all posts

Linq To Sql - Example 1 : Where -1

1:44 AM
Linq Query:

//Where -1
            NorthWindDataContext NwDC = new NorthWindDataContext();

            var cust =
                from c in NwDC.Customers
                where c.City.StartsWith("L")
                select c;
            foreach (var t in cust)
            {
                Console.WriteLine("Name: {0} , City : {1} ", t.ContactName, t.City);
            }

//


Output:

Name: Thomas Hardy , City : London
Name: Christina Berglund , City : Luleå
Name: Victoria Ashworth , City : London
Name: Elizabeth Brown , City : London
Name: Ann Devon , City : London
Name: Martine Rancé , City : Lille
Name: Lino Rodriguez , City : Lisboa
Name: Alexander Feuer , City : Leipzig
Name: Simon Crowther , City : London
Name: Isabel de Castro , City : Lisboa
Name: Hari Kumar , City : London
Name: Art Braunschweiger , City : Lander
Name: Mary Saveley , City : Lyon

Sql Equivalent Query:

SELECT  ContactName
      , City
FROM    dbo.Customers
WHERE   city LIKE 'L%'


Output:

Thomas Hardy      London
Christina Berglund    Luleå
Victoria Ashworth    London
Elizabeth Brown     London
Ann Devon     London
Martine Rancé     Lille
Lino Rodriguez     Lisboa
Alexander Feuer     Leipzig
Simon Crowther     London
Isabel de Castro     Lisboa
Hari Kumar        London
Art Braunschweiger       Lander
Mary Saveley        Lyon


Explanation: We are simply listing all the Customer who are living in the city name Staring With 'L' letter.

For How to Setup Database and Linq to Sql Class Please follow the instructions here: Setting Up Linq to Sql Class

Linq To Sql Introduction and Creating NorthWind Database

4:20 AM
LINQ to SQL provides a runtime infrastructure for managing relational data as objects without losing the ability to query. Your application is free to manipulate the objects while LINQ to SQL stays in the background tracking your changes automatically.



Advantages L2S offers:


No magic strings, like you have in SQL queries
Intellisense
Compile check when database changes
Faster development
Unit of work pattern (context)
Auto-generated domain objects that are usable small projects
Lazy loading.
Learning to write linq queries/lambdas is a must learn for .NET developers.


Regarding performance:
Most likely the performance is not going to be a problem in most solutions. To pre-optimize is an anti-pattern. If you later see that some areas of the application are to slow, you can analyze these parts, and in some cases even swap some linq queries with stored procedures or ADO.NET.
In many cases the lazy loading feature can speed up performance, or at least simplify the code a lot.


Regarding debuging:
In my opinion debuging Linq2Sql is much easier than both stored procedures and ADO.NET. I recommend that you take a look at Linq2Sql Debug Visualizer, which enables you to see the query, and even trigger an execute to see the result when debugging.
You can also configure the context to write all sql queries to the console window, more information here


Regarding another layer:
Linq2Sql can be seen as another layer, but it is a purely data access layer. Stored procedures is also another layer of code, and I have seen many cases where part of the business logic has been implemented into stored procedures. This is much worse in my opinion because you are then splitting the business layer into two places, and it will be harder for developers to get a clear view of the business domain.




For Linq to Sql Session we will be working on Sql 's Northwind Database. 


Creating The Northwind Database:
You can Download The database from here: NorthWind  


The Complete backup of Northwind database:  Northwind

Now Next thing is to Create a New LINQ to SQL Data Model.
For all the Next Examples you will need This Class.

1. Create a  New Project Name is LinqToSqlExample.
2. Now Go To Project  -> Add New Item and Add a Linq To Sql File and Name it NorthWind.


3. You will see a screen like below.


Now we will add a Data Connection as shown in above figure in left pane.



Select Add Connection.. And you will see below screen.





Press ok and you will see below screen.




And now our class is created . we will be using this in our next examples.