Geeks With Blogs
Srijith Sarman Time,space and living


"LINQ will be a language intrinsic capability for universal query".

I have seen an interesting comment  in javalobby as a response to the above statement that came in an article regarding LINQ

It goes like this.

    "LINQ is solving a problem that's not a problem. There's no problem with accessing a DB with SQL, Java objects with Java code, and XML data with a DOM or SAX parser. No need to "unify" these."

Even there exists some confusion among the .Net programers ,about the need of  LINQ. We have always been told about the advantages of stored procedures and LINQ says otherwise. Both the arguments have supporters.

Here I am trying to discuss one feature of LINQ which I feel quite useful . That is querying over datasets.

I had faced situations to query datasets.. Join two datatables and retrieve result. Since there was no option then, We moved to other alternatives which of course assured more tedious. But LINQ handles this wonderfully. We can offer complex join operations in memory itself.

Let's look at why do wee need this. If we are using some enterprise databases such as Oracle or SQL Server, we can apply queries directly to databases. But,consider the scenarios when we have table informations in small DBs like SQlite,flat files,xmls etc. Performing complex queries against them would be out of question. If we could load these tables directly into dataset and apply queries over the dataset,life would be very easy, right?.

For example,

    var linqSample= from emp in employee
            join dept in department
            on emp.Field<int>("departmentID") equals dept.Field<int>("departmentID")
            where emp.Field<int>("age") > 30
            select new { employeeid = emp.Field<int>("employeeid"),
                       name = emp.Field<string>("employeename"),
                       department = dept.Field<string>("deptname"),
                       };


As we know,in C# 3.0 a new type var has been offered.


The above query performs an inner join between two tables ,employee and department.

We can even perform outer joins by using DefaultIfEmpty operator, like.


 var linqSample= from emp in employee
            join dept in department
            on emp.Field<int>("departmentID") equals dept.Field<int>("departmentID") into res
        from dept in res.DefaultIfEmpty()
            where emp.Field<int>("age") > 30
            select new { employeeid = emp.Field<int>("employeeid"),
                       name = emp.Field<string>("employeename"),
                       department = dept.Field<string>("deptname"),
                       };

Now,you decide whether LINQ is  only a kind of syntatic sugar coating as some people say .. Posted on Thursday, September 6, 2007 2:28 PM | Back to top


Comments on this post: Do we need LINQ?

# re: Do we need LINQ?
Requesting Gravatar...
good article
Left by nand on Jul 07, 2009 10:01 AM

# re: Do we need LINQ?
Requesting Gravatar...
I am appreciate to your answer but i have a question .
we are writing query at same page or code bheing , very similar in old fesaion.

Is there any performance problem with this syantax or to write a query at code bheind.?
Left by ANIL on Feb 06, 2010 12:01 AM

Your comment:
 (will show your gravatar)


Copyright © Srijith Sarman | Powered by: GeeksWithBlogs.net