Quantcast
Channel: SharePoint – Be Awesome @
Viewing all articles
Browse latest Browse all 19

Using LINQ to Group Join Data From 2 SharePoint Lists

0
0

One of the things you can now do with CAML in SharePoint 2010 is build a Join query.  However, there are some limitations.  The biggest limitation for using it to solve one of my problems was the fact that the two lists must be connected by a lookup field.  So if I had list A with a field of OrderID and list B included a lookup field that pointed to the OrderID field in list A, I could create a join with CAML.  If I had a third list, C, and this field also had a lookup to the OrderID field in list A, I could not use CAML to join the data from list B and C, even though they both share the common field, OrderID.

This scenario is plausible and possible using SQL.  It seemed that this would also be possible in LINQ given it has the ability to join data objects.  What I am describing here is done with LINQ to objects and not LINQ to SharePoint.  LINQ to SharePoint is converted to CAML when it runs.  Since I can’t do this in native CAML, I can’t do it in LINQ to SharePoint as well.

My data is stored in 2 SharePoint lists.  I get the items from the lists and convert this SPListCollection into a DataTable object.

DataTable ListBTable = oSPWeb.GetList(ListBUrl).GetItems(oSPQuery).GetDataTable();
DataTable ListCTable = oSPWeb.GetList(ListCUrl).GetItems().GetDataTable();

The tables are then converted to Enumerable objects so I can use them in the LINQ query.

var listC = ListCTable.AsEnumerable();
var listB = ListBTable.AsEnumerable();

var joinedData = listB.GroupJoin(listC, listBKey => listBKey.Field<string>("Customer"), listCKey => listCKey.Field<string>("Customer"), (listBResults, listCResults) =>
                new
                {
                    CustomerName = listBResults.Field<string>("Customer"),
                    CustomerAddress = listBResults.Field<string>("Address"),
                    CusomterOrder = listCResults.Select(a => a.Field<string>("Orders")).Count(),
                    TotalCost = Convert.ToInt32(listBResults.Field<string>("Price")) Convert.ToInt32(listCResults.Select(a => a.Field<string>("Quantity")))
                });

I used the lambda syntax here, but this will work with the ‘verbose’ notation as well. 

The query uses the GroupJoin method of the listB object.  This is because I want all of the data items from list B and records from list C that match.  A Left Outer Join in SQL terminology.  The first parameter is the list I am joining, the 2nd and 3rd are the fields I am using to matchup the two lists.  The last parameter contains the names of the two objects that hold the results.  Although joining the records from each list into one enumerable object, they are still referenced discretely.  I then create a new anonymous type, taking the fields I need from each list.  I also needed to do a calculation using a field from each of the two lists, which can easily be done.

The resulting object was used as the DataSource of third party control and displayed in a webpart.

In addition to the simple join here, I also needed to join on multiple key fields for another set of lists.  Inside the LINQ query, I created new anonymous types for parameters 2 and 3 using the fields I want to match.

  1. listBKey => new { customer = listBKey.Field<string>("Customer"), orderId = listBKey.Field<string>("OrderId") },
  2. listCKey => new { customer = listCKey.Field<string>("Customer"), orderId = listCKey.Field<string>("OrderId") },


Viewing all articles
Browse latest Browse all 19

Latest Images

Trending Articles





Latest Images