34

I have 2 lists. 1 is a collection of products. And the other is a collection of products in a shop.

I need to be able to return all shopProducts if the names match any Names in the products.

I have this but it doesn't seem to work. Any ideas?

    var products = shopProducts.Where(p => p.Name.Any(listOfProducts.
             Select(l => l.Name).ToList())).ToList();

I need to say give me all the shopproducts where name exists in the other list.

halfer
  • 18,701
  • 13
  • 79
  • 158
Martin
  • 22,234
  • 53
  • 190
  • 309
  • Thanks everyone for there comments! I have it working now... Thanks again.. Answer accepted. – Martin Mar 28 '11 at 12:34

5 Answers5

70
var products = shopProducts.Where(p => listOfProducts.Any(l => p.Name == l.Name))
                           .ToList();

For LINQ-to-Objects, if listOfProducts contains many items then you might get better performance if you create a HashSet<T> containing all the required names and then use that in your query. HashSet<T> has O(1) lookup performance compared to O(n) for an arbitrary IEnumerable<T>.

var names = new HashSet<string>(listOfProducts.Select(p => p.Name));
var products = shopProducts.Where(p => names.Contains(p.Name))
                           .ToList();

For LINQ-to-SQL, I would expect (hope?) that the provider could optimise the generated SQL automatically without needing any manual tweaking of the query.

LukeH
  • 242,140
  • 52
  • 350
  • 400
10

You could use a join, for example:

var q = from sp in shopProducts
        join p in listOfProducts on sp.Name equals p.Name
        select sp;

A fuller guide on join is here.

Jon Egerton
  • 36,729
  • 11
  • 90
  • 125
  • I hate the 'special' linq syntax, but conceptually the join is probably the way to go with linq-to-sql. With linq to object I'm not so sure which would perform better. – mavnn Mar 28 '11 at 08:55
  • Concerning linq-to-object - the query in this answer ultimately translates to an Enumerable.Join call, which according to the documentation has a hashed-lookup.http://msdn.microsoft.com/en-us/library/bb534675.aspx - "The default equality comparer, Default, is used to hash and compare keys." – devgeezer Jan 20 '12 at 18:46
4

You could create an IEqualityComparer<T> that says products with equal names are equal.

class ProductNameEqulity : IEqualityComparer<Product>
{
    public bool Equals(Product p1, Product p2)
    {
        return p1.Name == p2.Name
    }

    public int GetHashCode(Product product)
    {
        return product.Name.GetHashCode();
    }
}

Then you can use this in the Intersect extension method.

var products = shopProducts.Intersect(listOfProducts, new ProductNameEquality());
Matt Ellen
  • 9,933
  • 4
  • 61
  • 80
3

Try this please

var products  = shopProducts.Where(m=> listOfProducts.Select(l=>l.Name).ToList().Contains(m=>m.Name));
Theun Arbeider
  • 4,506
  • 11
  • 43
  • 64
2
var products = shopProducts
        .Where(shopProduct =>
                listOfProducts.Any(p => shopProduct.Name == p.Name))
        .ToList();
mgronber
  • 3,303
  • 14
  • 20