- LINQ (Language Integrated Query) is a Microsoft programming model and methodology that essentially adds formal query capabilities into Microsoft .NET-based programming languages.
- LINQ offers a compact, expressive, and intelligible syntax for manipulating data. The real value of LINQ comes from its ability to apply the same query to an SQL database, a Dataset, an array of objects in memory and too many other types of data as well.
- LINQ requires the presence of specific language extensions.
- LINQ uses a SQL-like syntax to make query expressions well beyond the capabilities of embedded SQL as implemented in programming languages.
- LINQ always works with objects so you can use the same basic coding patterns to query and transform data in XML documents, SQL databases, ADO.NET Datasets, .NET collections, and any other format for which a LINQ provider is available.
Step by Step Usage: –
⇒ Add Microsoft.SharePoint. LINQ Reference
⇒ Use the SPMETAL to generate the LINQ-to-SharePoint Data Context Class
C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\BIN>spmetal.exe /web:http://sp2010vm/namespace:SandboxedSolution.ProjectDashboardSummary /code:SPProjectInitiation.cs
This will create the SPProjectInitiation.cs in the C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\BIN and SPProjectInitiationDataContext as a Data Context. Please remember you have to regenerate this class every time you change the schema in the SharePoint Lists.
- Add the LINQ-to-SharePoint Data Context Class (e.g. SPProjectInitiation.cs) in your Visual Studio Project
- Write the Queries against the SharePoint List
SPProjectInitiationDataContext dc = new SPProjectInitiationDataContext(SPContext.Current.Site.Url);
var query = from c in dc.BusITCapitalBudget
where c. Year.Equals(strYear)
string strTargetCapitalBudget = “0”;
foreach (var c in query)
strTargetCapitalBudget = c.TargetCapitalBudget == null ? “0” : c.TargetCapitalBudget.ToString();