Quantcast
Channel: *****## R.MARAN. ## *****
Viewing all articles
Browse latest Browse all 39

Using Lambda Select,Where methods in SharePoint 2007 List Items

$
0
0

Lambda Expression contains lot of extended methods which works on collection. Here I have tried Select and Where method to fetch items from the SharePoint List.

SharePoint List :

Selecting Single value:

using (SPSite oSPSite = new SPSite("http://Moss2007Server"))
{
 using (SPWeb oSPWeb = oSPSite.OpenWeb())
    {
     SPList myList = oSPWeb.Lists
                           .Cast<SPList>()
                           .FirstOrDefault(l => 
                      Convert.ToString(l.Title) == "ExternalPeople");

if (myList != null)
       {
        var names = myList.Items.Cast<SPListItem>()
         .Where(itm => Convert.ToString(itm["Qualification"]) == "BE")                                                                                
         .Select(itm => Convert.ToString(itm["Title"]));
                                
        foreach (string str in names)
         {
           Console.WriteLine(str);
         }
 }
    }
}

Here .Select returns IEnumerable collection which can be Iterated using foreach
If we want .Select to return the SPListItem object then the statement should be like this.

Selecting Object:

var namesSPListItem = myList.Items.Cast<SPListItem>()
            .Where(itm => Convert.ToString(itm["Qualification"]) == "BE")                                                                               
                     .Select(itm => itm);                        
Console.WriteLine("************** Return as SPListItem **************");
foreach (SPListItem item in namesSPListItem)
{
   Console.WriteLine("First Name:"+item["Title"]);
   Console.WriteLine("Last Name:"+item["Last Name"]);
}

In the single value selection, we did not know what str is containing (whether FirstName or LastName). To have some readable and easily understandale we can change the above to use new operator along with .Select as below

var items = myList.Items
.Cast<SPListItem>()
.Where(itm => Convert.ToString(itm["Qualification"]) == "BE")
            .Select(n => 
                 new {
                        FirstName = Convert.ToString(n["Title"]),
                        LastName  =Convert.ToString(n["Last Name"])
                       }
  );

So in the Foreach we can use the above variables as below.
foreach (var item in items)
{
  Console.WriteLine("First Name:"+item.FirstName);
  Console.WriteLine("Last Name:"+item.LastName);
}

LINQ has a option to declare a temparory variable using let. This can be done in Lambda expression as below. In the following I would like to concat the FirstName, LastName and return as FullName.
var items1 = myList.Items.Cast<SPListItem>()
               .Where(itm => Convert.ToString(itm["Qualification"]) =="BE")
                .Select(n => 
             new {
                   FullName=string.Concat(n["Title"]," ",n["Last  Name"])
                 }
    );


Viewing all articles
Browse latest Browse all 39

Trending Articles