kamagra how much to take

Multiple SPListItemCollection Query With SPFieldLookupValue

While SPList objects do not sustain true relational integrity due to the inherent nature of unstructured SharePoint data storage (in the current version at least), it is common within business applications to use Lookup field types are leveraged to build weak references between SharePoint lists. As such, typed collection queries become important, combining LINQ with method chaining allows us to write succinct SPListItem returns.

Firstly, let’s make some requisite test proxy objects:

  1. SPWeb curWeb = SPContext.Current.Web;
  2. SPList list1 = curWeb.Lists["My First List"];
  3. SPList list2 = curWeb.Lists["My Second List"];

Now, let’s assume the following business requirement. I want to loop through list1, and match a returned SPField value to a string condition defined by a literal. Subsequently, the return will be used on the list2 SPList object within the Where clause. This is considered in the following code snippet:

  1. var resultSet  = list1.Items.Cast<SPListItem>()
  2. .Where(i => Equals (String.Compare(i["Property To Match #1"].ToString(), "Example String Literal"), 0))
  3. .SelectMany(x => list2.Items.Cast<SPListItem>()
  4. .Where(i => Equals(String.Compare(new SPFieldLookupValue(x["Client"].ToString()).LookupValue, (string) i["Property To Match #2"]), 0)));

The above code is arguable that the second casting operation for the typed collection build should be broken out into a separate variable before the call and replacing the Cast statement within SelectMany with the result of the call using ToList to forces immediate execution. Meh. :)

Share

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>