Angelo Badellino
Angelo Badellino

Reputation: 2201

How to implement an IN clause in LinQ

I have two ILIst of these objects:

    class ProductionMachineType
    {
        string code { get; set; }
        IEnumerable<string> ProductionToolsLink { get; set; }
    }

    class ProductionTools
    {
        string code { get; set; }
    }

I am looking for a fast Linq method that make me able to query the IList<ProductionMachineType> that contains at least one ProductionToolsLink contained inside the ILIst<ProductionTools>.

In SQL I would wite something like this:

SELECT 
      * 
FROM 
      IList<ProductionMachineType>
WHERE 
      IList<ProductionMachineType>.ProductionToolsLink IN ILIst<ProductionTools>

Is there a way to do this?

Upvotes: 0

Views: 692

Answers (2)

cjk
cjk

Reputation: 46465

This will do it, but I can't guarantee how efficient it is...

var output = machines.Where(machine => 
     machine.ProductionToolsLink
     .Any(link => tools.Select(tool => tool.code).Contains(link)));

Upvotes: 2

Restuta
Restuta

Reputation: 5903

Contains method can help you:

var names = new string[] { "Alex", "Colin", "Danny", "Diego" };

var matches = from person in people
        where names.Contains(person.Firstname)
        select person;

Upvotes: 5

Related Questions