LINQ - 如何选择从仅包含另一个列表的项目列表中的项目? [英] Linq - How to select items from a list that contains only items of another list?

查看:90
本文介绍了LINQ - 如何选择从仅包含另一个列表的项目列表中的项目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样两类:

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.对于例的状态为它上面的示例应该返回行与查询项目= 3。
我写这个查询:

Now i need to write a query that select the Items from my class that just have state of 1 and 2.For example for the sample above it should returns row with Item=3. i wrote this query:

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



但它返回项目= 1 either.Any主意?

but it returns Item=1 either.Any Idea?

推荐答案

这将返回所有国家都在 stateList 的项目,我认为这就是你所需要的:

This returns the items which all states are in stateList, I think that that's what you need:

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

这篇关于LINQ - 如何选择从仅包含另一个列表的项目列表中的项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆