c# - LINQ: return items in a List that matches any Names (string) in another list? -


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

If all products match names, then I should be able to return all the store products.

I have this but it does not seem to work any thoughts? Choose

  var product = shopProducts.Where (P => p.Name.Any (listOfProducts. (L = & gt; l.Name) .toList ())). ToList ();   

I really need to say that give me all the shops, where the name is present in another list.

Any help really appreciated

thanks

  var product = shopProducts.Where (p => listOfProducts.Any (l = & gt; p; name == l.Name)) .Oolist ();   

For LINQ-to-object, if there are many items in listOfProducts you can probably get a better performance if you A hashaseet & lt; T & gt; contains all required names and then use it in your query HashSet & lt; T & gt; Compared to O (n) for O (1) in , an arbitrary IEnumerable & lt; T & gt; .

  var name = new hashset & lt; String & gt; (ListOfProducts.Select (p = & gt; p.Name)); Var products = shopProducts.Where (P = & gt; names.Contains (p.Name)) .toList ();   

For LINQ-to-SQL, I hope (hopefully) that the provider can optimize the generated SQL automatically without the need of any manual tweaking of the query.

Comments