Asdfg
Asdfg

Reputation: 12213

compare lists and return common objects using LINQ

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication5
{
    class Program
    {
        static void Main(string[] args)
        {
            List<Customer> custList = new List<Customer>();

            custList.Add(new Customer { Name = "P1" });
            custList.Add(new Customer { Name = "P2" });
            custList.Add(new Customer { Name = "P3" });
            custList.Add(new Customer { Name = "P4" });
            custList.Add(new Customer { Name = "P5" });

            List<Customer> vendorList = new List<Customer>();
            vendorList.Add(new Customer { Name = "P1" });
            vendorList.Add(new Customer { Name = "P2" });

            //var v = custList.SelectMany(


        }
    }

    public class Customer
    {
        public string Name { get; set; }
    }

}

How do i copare these 2 lists and find only those customers who are present in custList as well as vendorList?

Upvotes: 5

Views: 12324

Answers (6)

Hogan
Hogan

Reputation: 70523

var both  = (from a  in custList 
             join b in vendorList on a.Name equals b.Name 
             select a);

Upvotes: 0

Anthony Pegram
Anthony Pegram

Reputation: 126864

From a comment to another answer:

Intersect expects both lists to be of same type. What if they are not of same type and what if property name is different in both lists? How do i use SelectMany here

In this case, you may wish to utilize Join. Such as

var result = from item1 in list1
             join item2 in list2
             on item1.Foo equals item2.Bar
             select new { item1, item2 }; // select whatever you need in the resultset

Upvotes: 7

James Michael Hare
James Michael Hare

Reputation: 38397

var commonList = custList.Intersect(vendorList);

All you need is IEqualityComparer to compare based on Name.

Upvotes: 0

Jon Skeet
Jon Skeet

Reputation: 1500665

Ideally, make your Customer class override GetHashCode and Equals (better yet, implement IEquatable<Customer>). Then you can just use:

var customerVendors = custList.Intersect(vendorList);

Otherwise, you'll to implement an IEqualityComparer<T> to compare customers for equality (e.g. by name, but you may choose other comparisons) and then use:

var customerVendors = custList.Intersect(vendorList, new CustomerComparer());

Note that these will both return IEnumerable<Customer>, which will be lazily evaluated. Sometimes that's what you want, but if you really need a List<Customer>, just call ToList() at the end, e.g.

var customerVendors = custList.Intersect(vendorList).ToList();

Upvotes: 13

Sjoerd
Sjoerd

Reputation: 75609

Use the Intersect method.

Upvotes: 1

BrokenGlass
BrokenGlass

Reputation: 160902

If your Customer class implements a "value like" Equals() you can just do:

var inbothLists = custList.Intersect(vendorList).ToList();

Otherwise you can use the overload that passes a custom IEQualityComparer that you can implement in a separate class (i.e. CustomerComparer) :

var inbothLists = custList.Intersect(vendorList, new CustomerComparer())
                          .ToList();

Upvotes: 4

Related Questions