Simply LINQ: What,Why and How.

database
Database @Image courtesy of ddpavumba/ FreeDigitalPhotos.net

Simply LINQ: What,Why and How.

Welcome to CodeSpread!

What is LINQ?

LINQ is Language-Integrated Query (LINQ). It is primarily an extension to the language’s capability where it adds query expressions which processes objects and data.

It is a one stop solution to all the query processing as now there is no need to learn a different query language for each type of data source: SQL databases, XML documents etc.

Why LINQ?

  • Type safe.
  • IntelliSense support.
  • Tables are available as classes.
  • SQL generated is well structured and optimized.
  • Targets Business rules.
  • Easy to debug with the visual studio’s debugger.
  • Easy to deploy as being a part of DLL.
  • LINQ to SQL parameterizes input so better security.
  • And,best of all, SIMPLE.

An example:

var Employee= from emp in Employees
where emp.name="CodeSpread"
select emp;

Let’s see the flow, First the ‘from’ clause points to the data source which is ‘Employees’. Next, ‘where’ specifies the filter which is the employee name. Lastly, ‘select’ statement returns the result set.

There are three main components of LINQ.

  • Standard Query Operators
  • Language Extensions
  • LINQ Providers

Standard Query Operators:

The standard query operators can be simply referred to as methods in LINQ .There are two sets of LINQ standard query operators,


var result= from emp in Employees
orderby emp.empid
select emp.name;

But it doesn’t look like method, let’s write it again.


var result= Employees.
Select(emp=>emp.name).OrderBy(emp=> emp.empid);

Looks good now. Both of the above examples just vary in syantax. There are many other Standard Query Operators available which we will enlist in a future article.

Language Extensions:

LINQ Providers:

These are data source-specific implementations for ex: in-memory object collections, Microsoft SQL Server databases, ADO.NET datasets and XML documents.

  • LINQ to Objects – Executes a LINQ query against a collection of objects

For an in-memory object collections like arraylist of employees,


var result= from emp  in empList where emp.name="CodeSpread" select emp;

  • LINQ to XML – Executes an XPATH query against XML documents.

XElement root= XElement.Load("\..\Employees.xml");

IEnumerable  employee= root.Elements();

  • LINQ to SQL – Executes LINQ queries against Microsoft SQL Server.

For a Database called company and table called customers.


IQueryable customer = from cust in company.Customers

where cust.name== "CodeSpread"  select cust;

  • LINQ to ADO.NET datasets- Executes LINQ queries against ADO.NET DataSets.

Same example as above where the data is queried from a dataset instead of directly from a table.

Author: hershey

A passion for knowledge drives me to do programming, A passion for programming drives me to create something different, A passion for creation drives me to spread the knowledge.

Share This Post On

0 Comments

  1. nice superb explaination

    Post a Reply
  2. thanks for the tutorial
    I am having a problem.
    The feature “Create SQL server database” isn’t available , I can’t select it.
    Can you help, please ?

    Post a Reply

Submit a Comment

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

More from CodeSpread:

  • IQueryable: Performance boost over remote Data AccessIQueryable: Performance boost over remote Data AccessIf we also follow the inheritance principle like OOPS programming, we need to extend our previous discussion “IEnumerable: Power behind Collections”. So this will lead us to write something about I...
  • C#: Anonymous Type is not anonymous!C#: Anonymous Type is not anonymous!Why Anonymous Type? Anonymous types provide you the ease of having set of properties (Read only) into a single object without knowing the type. It’s the job of compiler to generate the type inform...
  • C#: Useful JSON in .NetC#: Useful JSON in .NetWhy JSON? All web applications revolve around large chunk of data and availability of that data to our application is of utmost importance. To do that, we have options like fetch data from databas...
  • Few lines about WCF metadataFew lines about WCF metadataWhat is WCF metadata? WCF generates metadata for service endpoints and this metadata is used to describe how to interact with the service's endpoints. This information is used by Svcutil.exe to ge...
  • Short Note on Performance TestingShort Note on Performance TestingWikipedia says "performance testing is in general testing performed to determine how a system performs in terms of responsiveness and stability under a particular workload. It can also serve to inv...
  • SQL:My first SSIS PackageSQL:My first SSIS PackageI would like to share my experience about a situation, where I was given a task to export a file(say XML) from FTP location and get the data from this file populated into our database tables. Moreo...
  • 23 Dec: Must Read Codes [11-15]23 Dec: Must Read Codes [11-15]We are sharing few must read codes/concepts which are required now and then. Each of these codereads doesn’t require a full length descriptive article so combining them into groups of five. CodeRe...
  • How to view Intermediate Code in C# using Ildasm.ExeHow to view Intermediate Code in C# using Ildasm.ExeHi guys, this is Sathish again, today let’s discuss how to view the intermediate code in c#. We know that CLR (Common Language Runtime) is used to convert the MSIL (Microsoft Intermediate Language...