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
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;






Recent Comments