LINQ Query to SharePoint List using Contains with Array


SPMetal tool folder location

C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\BIN\

Command to create a Entity class for the site

SPMetal /web:http://<servername>/site /namespace:<namesp> /code:filename.cs

ex: SPMetal /web:http://senthamil/sites/sample /namespace:Sample /code:Sample.cs

The sample.cs file will be generated with the Lists and the content type information. To compare with the array of document names in the document library, a LINQ query can be used to filter the document.

List<string> docNames= new List<string>();

List<string> docExt= new List<string>();

docNames = {“Contract”, “Agreement”,”Case Study”, “Circular”};

docExt = {“.doc”, “.docx”,”.xls”, “.xlsx”};

Sample.SampleDataContext ctx = new Sample.SampleDataContext(SPContext.Current.Web.Url);

////The LINQ query to filter the documents with the specific name and extension

var result = from docs in ctx.MyDocuments.ScopeToFolder(“”,true) /// Get all the documents irrespective of folders

              where docnames.Contains(.docs.Name) && docExt.Contains(docs.Name)

              select docs;

The above code will filter the document based on name and extention from the MyDocuments document library in the Sample sharepoint site. With just single easy to understand query able to filter documents based on dynamic criteria

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s