Posted by: R Manimaran | July 25, 2011

SharePoint: Join Query using Lambda Expression in SharePoint List

SharePoint: Join Query using Lambda Expression in SharePoint List

Join: Join is the extension method which works on the IEnumerable

I found many articles and examples which uses LINQ to join two tables. I tried the same using Lambda Expression. For tables I have used SharePoint List. The Join method operates on the SPListItem and Join two SharePoint List.

I have created two SharePoint List

· Test

· Countries

Test


Countries

Here “Test” List contains a Lookup column (Country) which refers the “Countries” List.


SPList myList = oSPWeb.Lists.Cast()

.FirstOrDefault(l => l.Title == "Test");

SPList myCountryList = oSPWeb.Lists.Cast()

.FirstOrDefault(l => l.Title == "Countries");

var items = myList.Items.Cast().Join

(myCountryList.Items.Cast(),

(l1 => ReturnLookupValue(Convert.ToString(l1["Country"]))),

(l2 => Convert.ToString(l2["Title"])),

((l1, l2) => new { l1, l2 }));

foreach (var item in items)

{

Console.WriteLine(item.l1.Title.ToString() +"-"+ item.l2.Title);

}

Here in the above I am using the columns “Title” from Countries List and “Country” Column from the Test List. As the Lookup value will return the value along with the LookupId(#1;India), I removed the ID using the method ReturnLookupVale.

ReturnLookupValue method will contain the following code to return the lookup value.


SPFieldLookupValue lookup = new SPFieldLookupValue(LookupValue);

return lookup.LookupValue;

Advertisements

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s

Categories

%d bloggers like this: