In SharePoint 2010 we have the ability to use LINQ syntax to fetch items from your lists instead of using the “traditional” approach of CAML queries.
Here, I will give you a brief introduction to how you can get started using LINQ queries in SharePoint, also known as LINQ to SharePoint.
Please see MSDN for LINQ Basics.
Now Lets start the cream part of this post “LINQ to SharePoint”
LINQ to SharePoint!!!
To work with LINQ in SharePoint 2010, SPMetal.exe tool is available which resides in the 14 hives bin folder (C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\BIN). SPMetel.exe is used to generate some entity classes which VS2010 can use to get IntelliSense, and allows for LINQ-based queries to be perfomed on SharePoint Sites lists.
•LINQ to SharePoint queries are translated to proper CAML queries
•CAML queries are in turn later translated to SQL queries
We will generate our entity-classes that are needed to perform these object oriented queries toward our SharePoint server using SPMetal.exe tool as below.
Launch a cmd-window with “run as administrator” and navigate to C:Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\bin
To utilize SPMetal.exe tool we have to execute command as below
This command will create “MySPLinqEntities.cs” file on Drive C: (You can change drive as well). So Navigate to Drive C: and find the created .cs file using SPMetal.exe
Now Open up the file and take a look at the content that SPMetal now have provided us with
Look into above image and you find the class name is now MySPLinqEntitiesDataContext. It is based on the name you specify as your code file in the SPMetal.exe command line tool.[You can use any name, DataContext keyword will be appended with your file name]
We are done with File creation [Base is created to use LINQ in SharePoint]. We need to do is import it to one of our projects and use it!
Read this post for Web Part that utilizes LINQ to SharePoint