Ask your technical questions on forums or here :
ASP.NET or MVC | C# | Windows Phone
Microsoft Technology Journals by Abhimanyu K Vatsa
HOME ABOUT RAZOR BOOK SPEAKING MVC ASP.NET JQUERY VIDEOS EBOOK ARCHIVE
Showing posts with label Linq. Show all posts
Showing posts with label Linq. Show all posts

28 Oct 2012

Filter Records in MVC

In this quick post you will learn various ways to filter records in MVC. Before starting let’s have a quick look at a controller and view page that I will use in this article. I will be using the SQL Server Northwind sample database in this article, you can download it from the Microsoft website and set up the same project to explore it.

8 Oct 2012

Entity Framework Console Applications with SQL Server Compact

Microsoft SQL Server Compact Edition is an embedded database system that allows us to integrate it in our Web, Desktop and Mobile applications.

Entity Framework's Database Seed Method

The Entity Framework can automatically create/update/drop databases when the application runs. We can specify that, this should be done every time application runs or only when the model is out of sync with the existing database or in other word it runs whenever model changes. You spent time to insert records in database and when you made any changes in model, Entity Framework deletes your database as well as records.

5 Oct 2012

Mapping Table-Valued Functions (TVFs) using Entity Framework Database First

Entity Framework 5 brings number of improvements and Mapping Table-Valued Functions (TVFs) Support is one of them.

Spatial Data Type Support in Entity Framework 5

Entity Framework 5 brings number of improvements and Spatial Data Type Support is one of them.

8 Sep 2012

Free ebook: Beginning LINQ



A new fashion to write Queries and address the impedance mismatch between programming languages and database.

This e-book is based on my series of LINQ posts and I’m just consolidating all blog posts here.



PDF Version


Download Size: 1.10 MB
Total Pages: 36





Written by Abhimanyu Kumar Vatsa (itorian.com) and Edited By: Deepak K Gupta.

7 Sep 2012

LINQ Query Syntax and Method Syntax - Part 11

This is eleventh part of the ‘LINQ’ series posts that I have started from here. In the last post you learnt how to perform calculations in LINQ query. Now, in this post you will look at some differences between LINQ Query Syntax and Method Syntax used in LINQ.

5 Sep 2012

Performing Calculations in LINQ Query - Part 10

This is tenth part of the ‘LINQ’ series posts that I have started from here. In the last post you learnt how to transform source data/object into XML file and in this post will go ahead and learn how to perform some calculations in LINQ query.

4 Sep 2012

Transforming Data Source objects into XML using LINQ - Part 9

This is ninth part of the ‘LINQ’ series posts that I have started from here. In the last post we explored customizing the LINQ’s ‘select’ statement to select subset of each source element. Now, in this post you will learn how to transform data source objects into XML.

Customizing LINQ’s ‘select’ statement - Part 8

This is eighth part of the ‘LINQ’ series posts that I have started from here. In the last post we explored how to join data from multiple data sources using ‘concat’ key. Now, in this post you will learn how to customize the LINQ’s ‘select’ statement to select subset of each Source Element.


Joining Multiple Data-Sources using 'Concat' Key in LINQ - Part 7

This is seventh part of the ‘LINQ’ series posts that I have started from here. In the last post we explored how to filter, order, group and join in LINQ. Now, in this post we will take a look at concate key used LINQ query to do joins.

3 Sep 2012

Simple demo project on LINQ to SQL using Console Application

In this video post, you will learn how to setup a simple project on LINQ to SQL using Console Application.

LINQ (Language Integrated Query) - Part 6

This is sixth part of the ‘LINQ’ series posts that I have started from here. In the last post we explored selecting records using LINQ and its internals. Now in this post we will be looking at filtering, ordering, grouping and joining using LINQ.

1 Sep 2012

LINQ (Language Integrated Query) - Part 5

This is fifth part of the ‘LINQ’ series posts that I have started from here. In the last post we talked on how to setup a demo project to explore LINQ queries, I expect you too have a demo project with you, if not then visit Part 4 of this series and setup it. Let’s go ahead and explore selecting records using LINQ and explore internals.

31 Aug 2012

LINQ (Language Integrated Query) - Part 4

This is fourth part of the ‘LINQ’ series posts that I have started from here. And in this post, you will learn how to setup a demo project to explore LINQ queries. I will use my preinstalled ‘Northwind’ database that you can download from here and install it in SQL Server Management Studio.


LINQ (Language Integrated Query) - Part 3

This is third part of the ‘LINQ’ series posts that I have started from here. And in this post, you will learn something on ‘Generic’ like what are generic types, why we need it in LINQ?


30 Aug 2012

LINQ (Language Integrated Query) - Part 2

This is second part of the ‘LINQ’ series posts that I have started from here. And in this post, you will see some quick examples on LINQ title.

As I said in previous post, with LINQ you just need to perform three distinct actions: Obtain the Data Source, Create the query and Execute the query, you will notice it in sample programs given below. We will create Console Apps and test various LINQ concepts. I will walk through very simple programs here and in coming part will dig in depth.

23 Aug 2012

LINQ (Language Integrated Query) - Part 1

Recently I had started exploring ‘LINQ to Entities’ new features. So, I thought to start a series of posts on ‘LINQ’ title and accumulate it on my blog for the beginner’s.

Hope, this series will be helpful to you to understand the ‘LINQ’ and before jump start to the application development let’s talk on basics something like: what is linq and its benefits.

25 May 2012

Query Expression vs Dot Notation in LINQ (a concise talk)


Introduction

We can see there are two or more ways to achieve the same results in LINQ that is by using dot notation and query expression.

Look at the sample examples.

Query Expression Example

var results = from product in products
                orderby product.Price descending
                select new
                {
                    product.Name,
                    product.Price
                };
 
int count = 0;
// print out the names
foreach (var p in results)
{
    Console.WriteLine("Item: {0}, Cost: {1}", p.Name, p.Price);
    if (++count == 3)
    {
        break;
    }
}


Dot Notation Example

var results = products
                        .OrderByDescending(e => e.Price)
                        .Take(3)
                        .Select(e => new { e.Name, e.Price });
 
foreach (var p in results)
{
    Console.WriteLine("Item: {0}, Cost: {1}", p.Name, p.Price);
}


The idea is that there are two ways to express the same thing, but which way is the best?

The "dot" notation is usually called Lambda syntax. The first notation goes by a number of names but I usually call it the query syntax.

As I know, in general, the more seasoned (with LINQ) developers migrate towards the Lambda syntax but there are significant exceptions.

Lambda is more concise but performing multiple table joins is a nightmare. Joins are just much cleaner with the query syntax. The flip side is that there are a number of LINQ operations that only exist within the Lambda syntax: Single(), First(), Count() etc.

What is LINQ? Learn it by solving problems.


Introduction

LINQ (Language INtegrated Query) fills the gap between programming languages and database.

According to Anders Hejlsberg (C# Chief Architect):-

“Microsoft’s original motivation behind LINQ was to address the impedance mismatch between programming languages and database.”

If we compare LINQ to SQL, LINQ is simpler, tidier and very handy to use. It’s rather like comparing C# to C/C++ Programming, the time is gone when C/C++ was best but now coder needs something special like LINQ and it’s a big win. SQL is a very old language that was invented about 1974. Since then it's been extended endlessly, but never redesigned. This has made the language messy. You might have become so accustomed to this that you can't see anything wrong.

I think my above lines will increase your concrete knowledge about Microsoft LINQ, so let’s look at some problems and we will solve this using LINQ and non-LINQ.

Question: We have a collection of Student objects and we want to find out three students who have minimum age.

Look at the non-LINQ Solution:-

using System;
using System.Collections.Generic;
 
namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            // create the object of the class and assigning values
            Student[] std = {
                                  new Student { Name = "Abhimanyu", Age = 21 },
                                  new Student { Name = "Deepak", Age = 10 },
                                  new Student { Name = "Mahtab", Age = 15 },
                                  new Student { Name = "Ravi", Age = 12 },
                                  new Student { Name = "Avnish", Age = 13 }
                              };
 
            // define the array to hold the results
            Student[] results = new Student[3];
 
            // short the contents of the array
            Array.Sort(std, (student1, student2) => {
                return Comparer<decimal>.Default.Compare(student1.Age, student2.Age);
            });
 
            // get the first three students in the array as the results
            Array.Copy(std, results, 3);
 
            // print out the name
            foreach (Student s in results)
            {
                Console.WriteLine("Name: {0}, Age: {1}", s.Name, s.Age);
            }
 
            Console.ReadKey();
        }
    }
 
    //class
    class Student
    {
        public string Name { get; set; }
        public int Age { get; set; }
    }
}
 
Now by using LINQ we can significantly simplify the querying process, look at example below.

using System;
using System.Linq;
 
namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            // create the object of the class and assigning values
            Student[] std = {
                                  new Student { Name = "Abhimanyu", Age = 21 },
                                  new Student { Name = "Deepak", Age = 10 },
                                  new Student { Name = "Mahtab", Age = 15 },
                                  new Student { Name = "Ravi", Age = 12 },
                                  new Student { Name = "Avnish", Age = 13 }
                              };
 
            // using LINQ
            var results = from s in std
                          orderby s.Age ascending
                          select new
                          {
                              s.Name, s.Age
                          };
 
            int count = 0;
            // print the name
            foreach (var a in results)
            {
                Console.WriteLine("Name: {0}, Age: {1}", a.Name, a.Age);
                if (++count == 3)
                {
                    break;
                }
            }
            Console.ReadKey();
        }
    }
 
    //class
    class Student
    {
        public string Name { get; set; }
        public int Age { get; set; }
    }
}
 
Remember to add a namespace using System.Linq.

This is lot neater. You can see SQL like query is used that is LINQ. We order the student objects in ascending and descending order and use the select keyword to return an anonymous type that contains just the properties we want. This style of LINQ is known as query syntax and it is the kind most developers familiar with.