Posts

Showing posts from July, 2011

LinQ to SharePoint examples

Namespaces to refer in Visual studio for using LinQ

using System.Linq;
using Microsoft.SharePoint.Linq;
using System.Collections.Generic;

Generate Entity classes using SPMetal
Run the following command to generate it
spmetal /web:http:// /code:

Creating a Join between two list UserAccounts and Invoices.
List UserAccounts have Account, Name
List Invoices have Account, Amount, Paid

//First refer the Datacontext class
XDataContext a = new XDataContext(SPContext.Current.Web.Url);


//Refere the Lists and create entityLists out of them

EntityList Invoices = a.GetList("Invoices") ;
EntityList UserAccounts = a.GetList("UserAccounts");

//need to do a join from the two lists and bring them together.
//use the ToList() on the list in the from to avoid getting errors due to inefficient CAML queries. with this method it retrives data from the list instantaniously.

var query=
(from u in UserAccounts.ToList()
join i in Invoices on u.Account equals i.Account
where int.Parse(u.Account.…