【问题标题】:How Query for contain any list如何查询包含任何列表
【发布时间】:2015-07-02 20:21:29
【问题描述】:

我有一个班级组和一个列表

我包括dotnetFiddle

class Group
{
    public List<int> manyID;
    public int otherID;
}

    List<Group> allGroup = new List<Group>();
    Group g;

    g = new Group();
    g.manyID = new List<int>(new int[] { 1, 3 } );
    g.otherID = 1;
    allGroup.Add(g);

    g = new Group();
    g.manyID = new List<int>(new int[] { 0, 2, 4 } );
    g.otherID = 2;
    allGroup.Add(g);

    g = new Group();
    g.manyID = new List<int>(new int[] { 2, 3 } );
    g.otherID = 3;
    allGroup.Add(g);

我想过滤子组列表中出现的 manyID 中具有任何 ID 的所有组。

Group subGroup = new Group();
subGroup.manyID = new List<int>(new int[] { 0, 1 } ); 

对于单个 ID,我知道该怎么做。这只会带来第一组。

List<Group> filterGroup = allGroup
              .Where( a => subGroup.manyID.Contains( a.otherID))
              .ToList();

清单是什么?这应该带来前 2 组。

List<Group> filterGroup = allGroup
              .Where( a => subGroup.manyID.Contains( **a.manyID**))
              .ToList();

【问题讨论】:

标签: c# linq


【解决方案1】:

试试这个:

    filterGroup = allGroup
         .Where(a => subGroup.manyID.Intersect(a.manyID).Any())
         .ToList();

【讨论】:

    【解决方案2】:

    另一种解决方案:

    var filterGroup = allGroup
             .Where(a => a.manyID.Any(subGroup.manyID.Contains))
             .ToList();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-01-27
      • 2016-04-27
      • 1970-01-01
      • 2012-09-04
      • 2012-04-12
      相关资源
      最近更新 更多