Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/visual-studio-2008/2.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
C# 对象的筛选器列表,其中嵌套集合与数组匹配_C#_Arrays_Linq_Icollection - Fatal编程技术网

C# 对象的筛选器列表,其中嵌套集合与数组匹配

C# 对象的筛选器列表,其中嵌套集合与数组匹配,c#,arrays,linq,icollection,C#,Arrays,Linq,Icollection,我有一个对象列表(项),我想根据嵌套集合的值(特性,在objectgeneritem)对其进行筛选。作为过滤器的基础,我有一个int数组(filter)。我的目标是查找项目中的所有对象,其中功能集合至少包括过滤器数组中的所有值 public class GenericItem { public int Id { get; set; } public string Name { get; set; } public ICollection<Feature> Fe

我有一个对象列表(
),我想根据嵌套集合的值(
特性
,在object
generitem
)对其进行筛选。作为过滤器的基础,我有一个int数组(
filter
)。我的目标是查找
项目
中的所有对象,其中
功能
集合至少包括
过滤器
数组中的所有值

public class GenericItem {
    public int Id { get; set; }
    public string Name { get; set; }
    public ICollection<Feature> Features { get; set; }
}

public class Feature {
    public int Id { get; set; }
}

static void Main (string[] args) {

    var items = new List<GenericItem>();
    items.Add(new GenericItem() {
        Id = 1,
        Name = "Item A",
        Features = new Collection<Feature>() { 
            new Feature() {Id = 1},      
            new Feature() {Id = 2},      
            new Feature() {Id = 3}
        }      
    });
    items.Add(new GenericItem() {
        Id = 2,
        Name = "Item B",
        Features = new Collection<Feature>() {     
            new Feature() {Id = 2},      
            new Feature() {Id = 3}
        }      
    });
    items.Add(new GenericItem() {
        Id = 3,
        Name = "Item C",
        Features = new Collection<Feature>() {    
            new Feature() {Id = 3}
        }
    });

    int[] filter = new int[] {2, 3};

    var resultAll = items.Where(i => i.Features.All(f => filter.Contains(f.Id)));

    foreach (GenericItem I in resultAll)
        System.Console.WriteLine(I.Name);
}
以下是我在Stackoverflow上向其他人提供的许多解决方案。我遇到的问题是,在我的Linq查询(以及我尝试过的许多变体)中,我总是得到
项中的所有对象,其中所有
功能都包含在
过滤器中。我知道我的lambda表达式“顺序错误”,但是因为我想得到一个
GenericItem
列表,我似乎不知道如何编写表达式

如何编写Linq表达式以获得预期结果

因此,在下面,当我筛选
[2,3]
数组时,我的目标是获得
结果
保存“项目A”和“项目B”(两者至少都有特征2和3)。相反,我得到的是“项目B”和“项目C”的
结果,因为它们的
特性都包含在
过滤器
数组中

public class GenericItem {
    public int Id { get; set; }
    public string Name { get; set; }
    public ICollection<Feature> Features { get; set; }
}

public class Feature {
    public int Id { get; set; }
}

static void Main (string[] args) {

    var items = new List<GenericItem>();
    items.Add(new GenericItem() {
        Id = 1,
        Name = "Item A",
        Features = new Collection<Feature>() { 
            new Feature() {Id = 1},      
            new Feature() {Id = 2},      
            new Feature() {Id = 3}
        }      
    });
    items.Add(new GenericItem() {
        Id = 2,
        Name = "Item B",
        Features = new Collection<Feature>() {     
            new Feature() {Id = 2},      
            new Feature() {Id = 3}
        }      
    });
    items.Add(new GenericItem() {
        Id = 3,
        Name = "Item C",
        Features = new Collection<Feature>() {    
            new Feature() {Id = 3}
        }
    });

    int[] filter = new int[] {2, 3};

    var resultAll = items.Where(i => i.Features.All(f => filter.Contains(f.Id)));

    foreach (GenericItem I in resultAll)
        System.Console.WriteLine(I.Name);
}
公共类泛型项{
公共int Id{get;set;}
公共字符串名称{get;set;}
公共ICollection功能{get;set;}
}
公共类功能{
公共int Id{get;set;}
}
静态void Main(字符串[]参数){
var items=新列表();
items.Add(新的GenericItem(){
Id=1,
Name=“项目A”,
Features=新集合(){
新功能(){Id=1},
新功能(){Id=2},
新功能(){Id=3}
}      
});
items.Add(新的GenericItem(){
Id=2,
Name=“B项”,
Features=新集合(){
新功能(){Id=2},
新功能(){Id=3}
}      
});
items.Add(新的GenericItem(){
Id=3,
Name=“C项”,
Features=新集合(){
新功能(){Id=3}
}
});
int[]filter=newint[]{2,3};
var resultAll=items.Where(i=>i.Features.All(f=>filter.Contains(f.Id));
foreach(结果中的通用项I)
系统控制台写入线(即名称);
}

All
应用于
过滤器
集合,而不是
i.Features

var resultAll = items.Where(i => filter.All(x => i.Features.Any(f => x == f.Id)));

感谢您的快速解决方案!