Best Online Tutorials

SQL Tutorial

Free SQL Tutorial

SQL Interview Questions

SQL Practice Questions

SQL Tips & Tricks

SQL Errors

SQL Books

Angular Tutorial

Free Angular Tutorial

Angular Interview Questions

Angular Practice Questions

Angular Tips & Tricks

Angular Errors

Angular Books

Node.js Tutorial

Free Node.js Tutorial

Node.js Interview Questions

Node.js Practice Questions

Node.js Tips & Tricks

Node.js Errors

Node.js Books

React.js Tutorial

Free React.js Tutorial

React.js Interview Questions

React.js Practice Questions

React.js Tips & Tricks

React.js Errors

React.js Books

SSIS Tutorial

Free SSIS Tutorial

SSIS Interview Questions

SSIS Practice Questions

SSIS Tips & Tricks

SSIS Errors

SSIS Books

SEO Tutorial

Free SEO Tutorial

SEO Interview Questions

SEO Practice Questions

SEO Tips & Tricks

SEO Errors

SEO Books

Javascript Tutorial

Free Javascript Tutorial

Javascript Interview Questions

Javascript Practice Questions

Javascript Tips & Tricks

Javascript Errors

Javascript Books

MongoDB Tutorial

Free MongoDB Tutorial

MongoDB Interview Questions

MongoDB Practice Questions

MongoDB Tips & Tricks

MongoDB Errors

MongoDB Books

Next.js Tutorial

Free Next.js Tutorial

Next.js Interview Questions

Next.js Practice Questions

Next.js Tips & Tricks

Next.js Errors

Next.js Books

LINQ Tutorial - LINQ Tutorial for beginners - DritalConnect 

Prashant Surya  |   Feb 27, 2020  |  Views: 123
pictures

Description:

What is LINQ?

A LINQ is a .Net framework concept which allows .Net languages to communicate with any database uniformally.

We don't use SQL queries to retrieve data from database.It has its own syntax which during the execution changes according too database used.

LINQ is a part of ADO.NET

It allows us to query against various data sources like SQL server, XML and collections and all other databases.

LINQ came with ASP.NET 3.5 framework.

It provides various functions like where etc. with arrays , so no needs to loops to select any array element.


Language Enhancement in ADO.NET 3.5:


1.  Object Declaration : (creating class)
2.  Object Initialization
3.  Function Invocation



Object Declaration: 
  • Automatic properties
  • Local type inference
Object Initialization:
  • Object Initializers
  • Collection Initializers
Function Invocation:
  • Extension Methods
  • Anonymous methods

 What are automatic properties? 

Properties which do not need to declare a private variable. 

The compiler automatically creates the private field & default get & set operations.


Local Type Inference: (use of var)


It allows to declare variables without telling their data types. Data type is determined during compilation based on the value assigned to it.

var a = 2;           // here a is declared as int
Object b = 2;    // Boxing an int into an object
int c = a ;          // possible because both are int type
int d = (int) b ;  // casting because b is n object & d is int 

Object Initializer:


Earlier to initialize objects we needed to call constructors but LINQ allows to do in a different way (no use of constructors)

Customer cust = new Customer { Name ='ABC', Country ='India'};


Difference between Constructors and properties :


In constructors we need to initilaize all the variables whereas initializing properties are optional.Thus object initializer allows partial declaration as it uses properties.


Collection Initializer:


Extension of object initilaizer.

List<Customer> list = new List<Customer>
                      { new Customer('x',28) {count='USA'},
        new Customer{Name='ABC'}
                      };


Extension Methods:


This method allows us to add more methods to the class without inheritance and without modifying the class code.


Lambda Expressions:


It is used instead of anonymous expression.
symbol:  => goes to
a => b means a goes to b
x => x*x will return square of x

It is used to create the delegates or expression tree types


class prgm
{
delegate int del (int i);
main()
{
del myDelegate = x => x*x ;  /* Using LINQ */
del md = delegate (int x)    /* Using anonymous function */
{
return x*x;
}
int j = myDelegate(5);
}
}

LINQ query expression is written using a declarative query syntax ( from visual c# 3.0)

LINQ syntax:
LINQ query syntax
LINQ method syntax

Query system are not understood by .Net CLR.

At compile time , query expression are translated to method calls.


Query Operator:


var expr = from c in customers
   where c.country == Countries.Italy
           select new { c.Name, c.city } ;

Here expr is of Iquerable type. It can contain multiple records.Now we can again query on it.


Projection Operator:


It means selection of columns.

var expr = customer.Select ( c=> c.Name );


Ordering operator:


orderby c.Name descending;


Grouping Operator:


group c by c.Country



Join operator:


.join(products, o=>o.PID, p=>p.PID, (o,p) => new {o.mnth,p.PID,p.price});


Set Operators:


Distinct - whole query.Distnct();
Union - var expr = c[i].orders.Union(c[2].orders);
Intersect - var expr = c[i].orders.Intersect(c[2].orders);
Except - var expr = c[i].orders.Except(c[2].orders);



Aggregate Operators:


Count - c.orders.Count()
Sum - int total = values.sum()
Min  - whole query.Min()
Max - whole query.Max()
Average - whole query.Avg()


If your table name is book then LINQ creates a class with name book and collection with name books.

ie. whatever be your table name it creates class with singular name & collection with plural name.


How to enable LINQ in your .Net application ?



To write query in LINQ we need to create DataContext ie an instance of DataBase.

Project Name -> right click -> Add new item -> LINQ to SQL Classes (.dbml extension) -> yes

Now got o Server Explorer -> DataConnections -> Add new connection ->

Create a new connection for DB to be used.


Now expend the database and drag and drop the tables to be used on the .dbml page -> save it and close it.

Now write the code in the .aspx.cs page.



In LINQ queries you can use the attributes of parent /child table very easily. You can go up to any level.

To use LINQ queries the tables shoulsd be properly structured / normalized & the tbles should be connected.

For LINQ queries , primary Key is must.

First - returns the first matching value
Single - returns value only if one value is there otherwise exception will be thrown.
select - returns multipe values but in boolean form. It is IQuerable.
where - returns multiple values & is IQuerable ie. returned vales can be furter used for query.
skip - This will skip one record from sellected records & return the rest records.
take - This will return only the given number of records.

var res = res.Skip(1).Take(2)


Grid supports Iquerable.

Dot after collection name gives methods whereas dot after classname gives column names.


How to change the column name of the output:


Lets suppose you want to replace qty column name by Quantity 

Quantity = x.qty

For float we use 'f'  and for decimal type we use 'm';

How to use 'like' in LINQ as used in SQl query:


p.Productname.StartsWith("s");
p.Productname.Contains("s");


How to call stored procedures using LINQ:


We can also call procedures using LINQ. To use them drag & drop them on .dbml page just like tables but they won't be visible.

To see them click on page (blank area) -> right click -> Show Methods Pane.

You can also delete dragged procedures from here.


Can we have multiple .dbml files in a project

 yes we can have.



var res =arr.take(3)   /* will etch only 3 records */
var res = arr.TakeWhile ( a=>a!=50)  /* will fetch results till a is not 50 */
var res = arr.SkipWhile ( a=>a!=50)  /* will skip records till a is not 50 */


when we refer to one level up then we get only one parent table so we can easily use column of parent table.

childTableName.ParenttableName.ColumnName of Parent Table

But if we move to one level down we will get multiple childs so we use aggregate functions on them.

p.OrderDetails.COunt > 2


Comments:
Content Contributor
Prashant Surya

Prashant Surya

Software developer at Uniserve Data Technologies Pvt. Ltd.  |   Noida, Uttar Pradesh, India

I am a software developer, freelancer and trainer working on technologies likes. MEAN stack, MERN Stack, ASP.NET full stack, WebRTC, socket.io ,PWA, SQL, JavaScript, jQuery, HTML, CSS, SEO, responsive UI/UX designing... having 7 years of experience in development in multiple domains like, eCommerce, RTA, capital markets, education, real estate...


Read related articles

What are Progressive Web Apps

Prashant Surya  |   Jan 27, 2021  |  Views: 75

How to generate leads from website

Prashant Surya  |   Jan 3, 2021  |  Views: 115

Best Live chat plugin for website for 2021

Prashant Surya  |   Dec 29, 2020  |  Views: 54

7 tips to engage your online visitors for 2021

Prashant Surya  |   Dec 29, 2020  |  Views: 92

What is next.js-What is next JS used for

Prashant Surya  |   Sep 13, 2020  |  Views: 35

What is Ajax

Prashant Surya  |   Feb 28, 2020  |  Views: 94

How to point a domain to another godaddy server

Prashant Surya  |   Dec 12, 2019  |  Views: 111

angular 7 tutorial for beginners step by step

Prashant Surya  |   Dec 11, 2019  |  Views: 342

angular tutorial app

Prashant Surya  |   Dec 11, 2019  |  Views: 211

angular 2 tutorial for beginners

Prashant Surya  |   Dec 11, 2019  |  Views: 227

angular 7 tutorial pdf

Prashant Surya  |   Dec 11, 2019  |  Views: 232

angular tutorial for beginners step by step

Prashant Surya  |   Dec 11, 2019  |  Views: 161

angular tutorial step by step

Prashant Surya  |   Dec 11, 2019  |  Views: 206

angular 6 tutorial dritalconnect

Prashant Surya  |   Dec 11, 2019  |  Views: 166

angular 4 tutorialspoint

Prashant Surya  |   Dec 11, 2019  |  Views: 238

angular 1.4 tutorial

Prashant Surya  |   Dec 11, 2019  |  Views: 184

angular 5 tutorial pdf

Prashant Surya  |   Dec 11, 2019  |  Views: 196

angular 5 tutorial for beginners step by step

Prashant Surya  |   Dec 11, 2019  |  Views: 162

angular tutorial project

Prashant Surya  |   Dec 11, 2019  |  Views: 200

angular tutorial online

Prashant Surya  |   Dec 11, 2019  |  Views: 173

angular 1.5 tutorial for beginners

Prashant Surya  |   Dec 11, 2019  |  Views: 177

angular 8 tutorial for beginners

Prashant Surya  |   Dec 11, 2019  |  Views: 280

angular 6 tutorial pdf download

Prashant Surya  |   Dec 11, 2019  |  Views: 204

angular 4 tutorial pdf

Prashant Surya  |   Dec 11, 2019  |  Views: 183

angular tutorial heroes

Prashant Surya  |   Dec 11, 2019  |  Views: 141

angular tutorial 6

Prashant Surya  |   Dec 11, 2019  |  Views: 182

angular 1.7 tutorial

Prashant Surya  |   Dec 11, 2019  |  Views: 166

angular tutorial visual studio

Prashant Surya  |   Dec 11, 2019  |  Views: 181

angular 8 tutorial for beginners step by step

Prashant Surya  |   Dec 11, 2019  |  Views: 214

angular tutorial with examples

Prashant Surya  |   Dec 11, 2019  |  Views: 162

angular tutorial visual studio 2017

Prashant Surya  |   Dec 11, 2019  |  Views: 154

angular tutorial example

Prashant Surya  |   Dec 11, 2019  |  Views: 163

angular tutorial 2019

Prashant Surya  |   Dec 11, 2019  |  Views: 147

angular 4 tutorial pdf download

Prashant Surya  |   Dec 11, 2019  |  Views: 185

angular tutorial 2019

Prashant Surya  |   Dec 11, 2019  |  Views: 147

angular tutorial for beginners step by step pdf

Prashant Surya  |   Dec 11, 2019  |  Views: 152

angular 9 tutorial

Prashant Surya  |   Dec 11, 2019  |  Views: 1484

angular 8 tutorial pdf

Prashant Surya  |   Dec 11, 2019  |  Views: 1808

angular tutorial book

Prashant Surya  |   Dec 11, 2019  |  Views: 166

angular tutorial pdf download

Prashant Surya  |   Dec 11, 2019  |  Views: 380

angular tutorial free

Prashant Surya  |   Dec 11, 2019  |  Views: 171

angular tutorial advanced

Prashant Surya  |   Dec 11, 2019  |  Views: 188

angular 6 tutorial dritalconnect

Prashant Surya  |   Dec 11, 2019  |  Views: 201

angular 6 tutorial pdf free download

Prashant Surya  |   Dec 11, 2019  |  Views: 377

angular tutorial c#

Prashant Surya  |   Dec 11, 2019  |  Views: 130

angular 7 tutorial pdf download

Prashant Surya  |   Dec 11, 2019  |  Views: 226

angular 1.2 tutorial

Prashant Surya  |   Dec 11, 2019  |  Views: 147

angular tutorial crud

Prashant Surya  |   Dec 11, 2019  |  Views: 191

angular 7 tutorial for beginners

Prashant Surya  |   Dec 11, 2019  |  Views: 172

angular 6 tutorial pdf

Prashant Surya  |   Dec 11, 2019  |  Views: 175

angular 6 tutorial for beginners step by step

Prashant Surya  |   Dec 11, 2019  |  Views: 228

angular 5 tutorial for beginners

Prashant Surya  |   Dec 11, 2019  |  Views: 157

angular 6 tutorial for beginners

Prashant Surya  |   Dec 11, 2019  |  Views: 166

angular tutorial pdf

Prashant Surya  |   Dec 11, 2019  |  Views: 162

angular tutorial for beginners

Prashant Surya  |   Dec 11, 2019  |  Views: 135

angular tutorial beginner

Prashant Surya  |   Dec 11, 2019  |  Views: 169

angular for beginners tutorial

Prashant Surya  |   Dec 11, 2019  |  Views: 196

angular 4.0 tutorial

Prashant Surya  |   Dec 11, 2019  |  Views: 177

angular tutorial 2

Prashant Surya  |   Dec 11, 2019  |  Views: 163

angular 4 tutorial

Prashant Surya  |   Dec 11, 2019  |  Views: 170

angular 2 tutorial

Prashant Surya  |   Dec 11, 2019  |  Views: 171

angular tutorial

Prashant Surya  |   Dec 11, 2019  |  Views: 190

How to take backup and restore mongoDB database

Pooja Goel  |   Dec 8, 2019  |  Views: 318

JavaScript Introduction

Prashant Surya  |   Sep 27, 2019  |  Views: 238

what to do if not selected in campus placement

Prashant Surya  |   Sep 22, 2019  |  Views: 533

Top 7 Market Leaders of Web Servers

Abhishek Roy  |   Sep 20, 2019  |  Views: 191

Top 10 most used eCommerce plugin or framework

Prashant Surya  |   Sep 19, 2019  |  Views: 93

Understanding Garbage Collection in Java

Swapnil Srivastava  |   Sep 16, 2019  |  Views: 480

Top 10 Side Income Ideas For Programmers

Prashant Surya  |   Sep 4, 2019  |  Views: 462

Want to sell your software ?-

If you want to sell your readymade software to the genuine clients or businessman, list your software with details and demo links.

  • Sell ERP
  • Sell Website Clones
  • Sell App Clones
  • Sell Readymade Portals
  • Sell Readymade Websites
  • Sell Mobile Apps
  • Sell Window Apps

Clients will find it using our advanced search filter and will contact you directly.

No any charge for the product lsiting.

Do you own a Company ?+

Property.sale

Buy or Sell Commercial Property+

Are you a freelancer ?+

Looking for a job ?+

Are you a Service Provider ?+

Are you a blogger ?+

Do you own an IT Institute ?+



Property.sale