Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/326.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

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
C# Linq-如何从只包含其他列表项的列表中选择项?_C#_Linq_Entity Framework - Fatal编程技术网

C# Linq-如何从只包含其他列表项的列表中选择项?

C# Linq-如何从只包含其他列表项的列表中选择项?,c#,linq,entity-framework,C#,Linq,Entity Framework,我有两门课: public class Item { public int Id{get;set;} public List<Test> TestList{get;set;} } public class Test { public int Id{get;set;} public Item Item{get;set;} public byte State{get;set;} } 和测试类数据: Id 1 2 3 Item State

我有两门课:

public class Item
{
   public int Id{get;set;}
   public List<Test> TestList{get;set;} 
}
public class Test
{ 
   public int Id{get;set;}
   public Item Item{get;set;}
   public byte State{get;set;}
}
和测试类数据:

Id
 1
 2
 3
Item   State
  1      1
  1      2
  1      3
  2      1
  2      4
  3      2
现在我需要编写一个查询,从类中选择状态为1和2的项。例如,对于上面的示例,它应该返回Item=3的行。 我写了这个查询:

var stateList=new List<byte>(){1,2};
Items.Where(x => x.TestList.Select(c => c.State).Any(s => stateList.Contains(s)));
var stateList=newlist(){1,2};
其中(x=>x.TestList.Select(c=>c.State).Any(s=>stateList.Contains));

但是它也会返回Item=1。你知道吗?

这会返回所有状态都在
状态列表中的项,我想这就是你需要的:

Items.Where(x => x.TestList.All(s => stateList.Contains(s.State)));

如果您只需要TestList中只有状态为2的项目的项目:

tems.Where( i => i.TestList.All(li => li.State == 2))

您是否也可以添加代码,以创建
项目
序列?Macrospus:当然不是!伊利亚·伊万诺夫:你为什么需要这个?!为什么它要返回带有
Item=3
的行,以便在本地执行查询并重现问题。你还想让我们怎样帮助你?纯粹是因为投机?