SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm trying to use values from 2 SPListItemCollections, as one of them have a lookup field with values from the other list.

The reason why I need to do this, is because I need the value from the other list in order to OrderBy correctly when populating the values.

This is what I have so far.

SPWeb web = SPContext.Current.Site.RootWeb;
SPList list1 = web.Lists["List1"];
SPList list2 = web.Lists["List2"];

foreach (var l1 in list1.Items.Cast<SPListItem>().OrderBy(l1 => l1["Category"]).ThenBy(l1 => l1["Position"]))
    /* Logic */

I want l1["Category"] to be replaced by a column name from list2.
How can I achieve this?

I'm not that experienced with "advanced" coding.

share|improve this question
up vote 3 down vote accepted

What you want to do is look into CAML field joins and projections

As the name suggests, it allows you to join two lists linked by a lookup field, and bring up the values through a single CAML query. The page linked above is not a beacon of clarity but you can find good examples on Google.

Using CAML will also enable you to put your Order by in it. A CAML query will also be more efficient than your current list1.Items.Cast<..>( )

Note also that using


can be slow if you have many lists on that web. Consider using something like

web.GetList(SPUtility.ConcatUrls(web.ServerRelativeUrl, "MyListUrl"));
share|improve this answer

You can achieve this by Join and Projected fields. Please check the section 3. List Joins here :

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.