LINQ – Language Integrated Query, It’s a new technology introduced in .NET 3.5. LINQ defines a set of proprietary query operators that can be used to query or filter data in arrays, enumerable objects, XML, Relational database and third party data sources.
It is extensively used by many .NET developers. I was exploring how LINQ can be used and what advantages it can bring in Sharepoint. I observed atleast two advantages of LINQ into Sharepoint
1) To some extent CAML queries can be replaced by LINQ :
CAML stands for Collaborative Application Markup Language which is used in Sharepoint technologies for many purposes and one of them is to query sharepoint lists/document libraries/Views. It’s bit complicated and error prone language. There are tools like U2U CAML Generator tool exist which can generate CAML for you but still developers may want to avoid writing CAML if there is an alternative exist and i found that LINQ can be a good alternative for querying sharepoint lists/views/document libraries. Also i think using CAML is heavier than using LINQ. Following example shows how simple and easy to write LINQ as compare to CAML.
2) Sharepoint lists can be joined using LINQ :
When i moved from .Net background to MOSS and when i had used SQL Server a lot to store/manipulate data in various .Net applications, I always had a question in my mind that how can i join Sharepoint Lists? Initially i was treating sharepoint list as a SQL table when i wasn’t aware it can do much more than that. And if you think it as a sharepoint table the very first question which will come to your mind is how can i relate two or more than two sharepoint lists. That leads to joins. Ok so the ultimate question is how the sharepoint lists can be joined? There are multiple ways to achieve it. One of it is using Sharepoint Designer, This link – http://blah.winsmarts.com/2007-10-Performing_joins_between_SharePoint_lists.aspx provides a good explanation of how it can be done using Sharepoint Designer. The another way i found is by using LINQ. Using LINQ you can do anything with Sharepoint list the same way you do with SQL Table meaning using LINQ you can sort,group, filter or join data from sharepoint lists.
Following code block explains how you can join Sharepoint lists named “Customers” and “Orders”. Also if you have used CAML to query sharepoint lists you can think that how LINQ can replace CAML to achieve the same thing.
using (SPSite site = new SPSite(SPContext.Current.Web.Url.ToString()))
{
using (SPWeb web = site.OpenWeb())
{
var CustomerItems = web.Lists[“Customers”].Items;
var OrderItems = web.Lists[“Orders”].Items;
//The resultset contains the results of joining two sharepoint lists
var JoinItems = from SPListItem c in CustomerItems
join SPListItem o in OrderItems on c[“Title”].ToString() equals
o[“CustomerName”].ToString()
where c[“Title”].ToString() == “Gary”
select new { Name = c[“Title”].ToString(), Product = o[“Title”].ToString()};
// The Results are being displayed in GridView
gv.DataSource = JoinItems;
gv.DataBind();
}
}
I have shared my thoughts of LINQ in Sharepoint. It can do much more than i am thinking but i am sure LINQ is powerful. Let’s see if Sharepoint 2010 brings any LINQ API(s) for us or it focus on LINQ.