?
Solved

Comparing 2 lists using Lambda

Posted on 2009-02-16
5
Medium Priority
?
2,822 Views
Last Modified: 2013-11-11
is this doable in Lambda/LINQ?

 have 2 un-ordered lists:

List1 ----   2, 1, 4, 6, 8, 9
List2 ---    1, 6, 9, 4,

List 2 has less items than List1. I want to compare the 2 lists and find the missing items. In this case, I should get back 8 and 2.

I did a loop like this:
 foreach (int values1 in List1)
 {
   foreach(int values2 in List2)
   {  
    if (values1 != values2)
        {  // do whatever
        }
    }
}
0
Comment
Question by:Camillia
  • 3
  • 2
5 Comments
 
LVL 21

Accepted Solution

by:
naspinski earned 2000 total points
ID: 23649344
I think you meant 2 and 8?

Here is an easy way to do it with linq:

IEnumerable<int> list3 = (List1.Count() > List2.Count()) ? List1.Except(List2) : List2.Except(List1);

Open in new window

0
 
LVL 7

Author Comment

by:Camillia
ID: 23649418
yes, 2 and 8. sorry. let me try it.
0
 
LVL 7

Author Comment

by:Camillia
ID: 23649664
that works, thanks. To get the values of List3, i did a foreach loop. cant repace the foreach with something else, correct?

 IEnumerable<Guid> deleteItems = (orderItemID.Count() > BasketItemID.Count()) ? orderItemID.Except(BasketItemID) : BasketItemID.Except(orderItemID);

                foreach (Guid di in deleteItems)
                {
                    Guid tobedeleted = new Guid(di.ToString());

                }
0
 
LVL 21

Expert Comment

by:naspinski
ID: 23649721
I would do it with a foreach - you could also use a for loop, but I don't see why you would.
0
 
LVL 7

Author Comment

by:Camillia
ID: 23649759
thanks for help.
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Suggested Courses
Course of the Month16 days, 23 hours left to enroll

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question