AscenWork Technologies

What is LINQ. How is it used in SharePoint?

What is LINQ. How is it used in SharePoint?

August 18, 2018 | By Shripal Shah

Blog single SharePoint

What is LINQ?

  • 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 it 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.
  • It requires the presence of specific language extensions.
  • Uses a SQL-like syntax to make query expressions well beyond the capabilities of embedded SQL as implemented in programming languages.
  • 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) select c;

string strTargetCapitalBudget = “0”; foreach (var c in query) { strTargetCapitalBudget = c.TargetCapitalBudget == null ? “0” : c.TargetCapitalBudget.ToString(); }