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 have 2 custom lists of same structure(brandname column).I need to compare each item of 2 lists and print the mismatched items of 2nd list.

Can any one show me with caml query example.

I have written like.

foreach(SPListitem item1 in list1.items)
foreach(SPListitem item2 in list2.items)
share|improve this question
up vote 3 down vote accepted

In addition to what James said, I think what you trying to do won't going to work the way you are doing it.

You should get all items from both lists and store them in List classes using SPQuery and then you can go through lists and get items from list 2 which are not present in list 1.

SPQuery will look something like this,

 SPWeb web = SPContext.Current.Web;
            SPList list = web.Lists["Employee"];
            string query = @"<Where>
                                                    <FieldRef Name='Position' /><Value Type='Choice'>{0}</Value>
                                            <FieldRef Name='Salary' Ascending='False' />
            query = string.Format(query, "Developer");                                                            
            SPQuery spQuery = new SPQuery();
            spQuery.Query = query;
            SPListItemCollection items = list.GetItems(spQuery);

You can then check list classes you created using list.Contains method,

public class List1
    public List1() { }

    public string BrandName { get; set; }


public class List2
    public List2() { }

    public string BrandName { get; set; }

Create two instances of classes like,

List1 ListOne = new List1();
List2 ListTwo = new List2();

The items you get from SPQuery, you can add each item's brandName to list like this,

Foreach(SPListItem item in SPListItemCollectionVariable)
    listOne.BrandName = item.BrandName; //or whatever property name is

Do same for List Two and then you can check if item is there or not,

foreach(ListItem ii in listTwo)

Since I typed most of code on a notepad to give you an idea of how it should work so there might be some minor mistakes that you can take care of.

share|improve this answer
Rather than two classes ListOne and ListTwo, I'd create just two List<string> objects - the classes you've suggested don't really represent collections. – James Love Mar 2 '13 at 19:34
@JamesLove that's true, he can only use one class and create two instances of it, and reason I suggested class is because then he can add as many properties he want in future and also can extend class with I-Comparable method for sorting :) – Muhammad Raja Mar 2 '13 at 20:50
I did like List<List1> firstlist=new List<List1>();and List<List2> secndlist=new List<List2>(); then inside foreach firstlist.Add(item.BrandName);.thanks both of you. – Neel Mar 3 '13 at 4:02

First thing - it's not a good iterate over SPList.Items, it can be horrendously ineffecient, especially if your list has many items and/or many columns. Build an SPQuery object to get only the items and fields you need.

Refer to this MSDN article with a coded example on how to query lists:

(I'm aware this in a Console App and thus performance isn't a huge concern, unless you plan on making this a repeating job - in which case you're better writing this in PowerShell to simplify maintenance).

Next - to answer your question, I would order both lists by a common field. If you have one item that matches another, break out that iteration for that item. You're interested in fields that DON'T match, so if the loop for that item completes, write out details about that item - as it's the current item that doesn't seem to match any in your target list.

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.