---
title: "Using LINQ to access SharePoint list Data"  
description: "Using the LINQ to SharePoint provider is a way to add and read items from a Microsoft SharePoint 2010 list. In SharePoint 2010 we can also use LINQ sy"  
author: "Chris Anderson"  
published: 2011-12-09  
updated: 2020-03-04  
canonical: https://www.mindstick.com/articles/842/using-linq-to-access-sharepoint-list-data  
category: "sharepoint"  
tags: ["sharepoint"]  
reading_time: 2 minutes  

---

# Using LINQ to access SharePoint list Data

Using the LINQ to SharePoint provider is a way to add and read items from a Microsoft SharePoint 2010 list. In SharePoint 2010 we can also use LINQ syntax to fetch items from your lists instead of using the SPSiteDataQuery and SPQuery objects.\

In this article I will give you a brief [introduction](https://yourviews.mindstick.com/view/85450/mesopotamia-introduction-to-an-ancient-civilization) to how you can get started using [LINQ queries](https://www.mindstick.com/forum/159873/how-can-i-optimize-linq-queries-for-better-performance-in-web-applications) in SharePoint, also [known as](https://answers.mindstick.com/qa/38482/name-the-world-s-lightest-material-which-was-manufactured-by-isro-scientists-at-the-vikram-sarabhai-space-centre-thiruvananthapuram-and-is-also-known-as-blue-air) **LINQ to SharePoint**.

In order to work with LINQ in SharePoint 2010, we need use a tool called **SPMetal.exe** which resides in the **C:\Program Files\Common Files\Microsoft Shared\[Web Server](https://www.mindstick.com/articles/23199/digital-personal-server-the-premium-web-server) [Extensions](https://www.mindstick.com/articles/12362/is-ajax-cart-extensions-necessary-for-e-commerce-websites)\14\BIN** folder.\
Using the tool called **SPMetal**, we generate our entity-classes that are needed to perform these [object oriented](https://www.mindstick.com/articles/12072/features-of-java-simple-small-and-object-oriented) queries toward our [SharePoint server](https://www.mindstick.com/forum/320/sharepoint-server-displays-a-blank-page-after-installing-sharepoint).

Open a cmd-window and navigate to **C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\bin**

![Using LINQ to access SharePoint list Data](https://www.mindstick.com/mindstickarticle/9ac47341-30ea-409c-8c39-4d2740929720/images/8b7f273e-9384-44d0-bdab-7580356990c9.png)

Run the following command in command prompt:

Syntax:**SPMetal.exe /web:http://sharepointsiteaddress /code:d:\YourEntityFile.cs**

![Using LINQ to access SharePoint list Data](https://www.mindstick.com/mindstickarticle/9ac47341-30ea-409c-8c39-4d2740929720/images/2e945983-8b2e-4d08-b2a4-1e4fd445aae5.png)

- Open [Visual Studio](https://www.mindstick.com/articles/12378/visual-studio-for-mac-is-out-of-beta-preview-now-officially-available) 2010.
- Go to File à New à Project.
- Select [Console Application](https://www.mindstick.com/forum/160510/how-to-fetch-data-from-the-database-in-the-dot-net-console-application-using-c-sharp) from the installed templates.

![Using LINQ to access SharePoint list Data](https://www.mindstick.com/mindstickarticle/9ac47341-30ea-409c-8c39-4d2740929720/images/c30b254b-7e3d-4b31-8d80-c547d39472c2.png)

- Right click on the solution, select "Add an existing item".

- Add the MyEntities.cs class to the solution.

![Using LINQ to access SharePoint list Data](https://www.mindstick.com/mindstickarticle/9ac47341-30ea-409c-8c39-4d2740929720/images/40ec693e-98b8-4604-9a35-aba11fb67ad7.png)

· Add References by right click on the **Reference** option:

· Choose **Browse**:

· Go to the following location: **C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\ISAPI****.**

· Add **Microsoft.SharePoint.dll and Microsoft.SharePoint.Linq.dll**in Reference.

![Using LINQ to access SharePoint list Data](https://www.mindstick.com/mindstickarticle/9ac47341-30ea-409c-8c39-4d2740929720/images/80c69038-7076-4afb-87d2-3866f5f7c64b.png)

I have a following Products list in the SharePoint Server.

![Using LINQ to access SharePoint list Data](https://www.mindstick.com/mindstickarticle/9ac47341-30ea-409c-8c39-4d2740929720/images/527002f9-169a-480c-a8fa-c7548161ee2b.png)

Add the following code in Program.cs file to perform DML operation in SharePoint list:

```
using System;using System.Linq; namespace LinqinSP{    class Program    {        static void Main(string[] args)        {            using (MyEntitiesDataContext myEntitiesDataContext = new 
                                           
                                           MyEntitiesDataContext("http://rohit:34143/"))            {                var listItems = from items in myEntitiesDataContext.Products                                where items.Title.StartsWith("H")                                select new { items.Title };                foreach (var item in listItems)                {                    Console.WriteLine(item.Title.ToString());                }            }        }    }}
```

Output:

![Using LINQ to access SharePoint list Data](https://www.mindstick.com/mindstickarticle/9ac47341-30ea-409c-8c39-4d2740929720/images/9f720992-fd91-43f5-a439-1506288c121c.png)

Thanks for reading this article. I think this will help you a lot.

---

Original Source: https://www.mindstick.com/articles/842/using-linq-to-access-sharepoint-list-data

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
