Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在LinQ中实现IN子句_Linq_Generics_C# 3.0 - Fatal编程技术网

如何在LinQ中实现IN子句

如何在LinQ中实现IN子句,linq,generics,c#-3.0,Linq,Generics,C# 3.0,我有两个ILIst对象: class ProductionMachineType { string code { get; set; } IEnumerable<string> ProductionToolsLink { get; set; } } class ProductionTools { string code { get; set; } } 有办法做到这一点吗?包含方法可以帮

我有两个ILIst对象:

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

    class ProductionTools
    {
        string code { get; set; }
    }
有办法做到这一点吗?

包含方法可以帮助您:

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

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

这样就可以了,但我不能保证它的效率有多高

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

好的,这是正确的答案。效率问题是众所周知的,但我不能采取任何不同的方法,因为我们的领域模型不好。linq查询也相当复杂。所以我想我将使用两个foreach来提高清晰度。如果名称是另一个实体,它将输出一个EXISTS,而不是IN
var output = machines.Where(machine => 
     machine.ProductionToolsLink
     .Any(link => tools.Select(tool => tool.code).Contains(link)));