开发者

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<P开发者_开发百科roductionTools>

Is there a way to do this?


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;


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)));
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜