代码之家  ›  专栏  ›  技术社区  ›  Popo

比较两个列表,使用lambda谓词确定两个列表是否包含相同的项

  •  1
  • Popo  · 技术社区  · 7 年前

    如果我的对象列表包含vb中类型列表中的所有类型,我试图返回一个布尔值。net或C#。我正在努力写一个lambda表达式来实现这一点。使用lambda谓词可以实现吗?我知道用for-each循环可以很容易地完成。

    vb。网

    Public Class Widget
        Public wobbly As String
        Public sprocket As String
        Public bearing As String
    End Class
    
    Public Sub test()
        Dim wList As New List(Of Widget)
        wList.Add(New Widget() With {.bearing = "xType", .sprocket = "spring", .wobbly = "99"})
        wList.Add(New Widget() With {.bearing = "yType", .sprocket = "sprung", .wobbly = "45"})
        wList.Add(New Widget() With {.bearing = "zType", .sprocket = "straight", .wobbly = "17"})
    
        Dim typeList As New List(Of String) From {"xType", "yType", "zType"}
    
        Dim containsAllTypes As Boolean = wList.TrueForAll(Function(a) a.bearing.Equals(typeList.Where(Function(b) b = a.bearing)))
        Debug.WriteLine(containsAllTypes.ToString)
    End Sub
    

    C#

    public class Widget
    {
        public string wobbly;
        public string sprocket;
        public string bearing;
    }
    
    public void test()
    {
        List<Widget> wList = new List<Widget>();
        wList.Add(new Widget {
            bearing = "xType",
            sprocket = "spring",
            wobbly = "99"
        });
        wList.Add(new Widget {
            bearing = "yType",
            sprocket = "sprung",
            wobbly = "45"
        });
        wList.Add(new Widget {
            bearing = "zType",
            sprocket = "straight",
            wobbly = "17"
        });
    
        List<string> typeList = new List<string> {
            "xType",
            "yType",
            "zType"
        };
    
        bool containsAllTypes = wList.TrueForAll(a => a.bearing.Equals(typeList.Where(b => b == a.bearing)));
        Debug.WriteLine(containsAllTypes.ToString());
    }
    

    编辑,感谢所有的快速回答,我知道有几种方法可以做到这一点,现在对表达式中发生的事情有了更好的理解。

    7 回复  |  直到 7 年前
        1
  •  3
  •   Matt.G    7 年前

    尝试 var containsAllTypes = typeList.All(x => wList.Select(x => x.bearing).Contains(x))

        2
  •  2
  •   Scott Hannen    7 年前
    var containsAll = typeList.All(type => 
        wList.Any(widget => widget.bearing.Equals(type)));
    

    翻译过来,它适用于所有类型的 typeList 列表中的任何(至少一个)小部件都具有该类型。

        3
  •  2
  •   Michael Erickson    7 年前

    我相信你想要的是:

    bool containsAllTypes1 = wList.TrueForAll(a => null != typeList.Find(b => b == a.bearing));
    

    你也可以使用这个系统。Linq如下:

    bool containsAllTypes2 = wList.All(a => typeList.Any(b => b == a.bearing));
    
        4
  •  1
  •   RogerMKE    7 年前
     Dim containsAllTypes As Boolean = wList.All(Function(a) typeList.Any(Function(b) b = a.bearing))
    

    对于wList中的每个值,它会检查typeList中是否有任何值与wList轴承值匹配。

        5
  •  1
  •   tschmit007    7 年前

    越短越好

    containsAllTypes = wList.Where(x => typeList.Contains(x.bearing)).Count() == typeList.Count;
    

    或

    containsAllTypes =  wList.Select(x => x.bearing).Except(typeList).Count() == 0;
    

    或

    containsAllTypes =  wList.Select(x => x.bearing).Intersect(typeList).Count() == typeList.Count;
    
        6
  •  0
  •   Aleks Andreev Md. Suman Kabir    7 年前

    你可以用 Intersect 检查两个列表是否具有相同的值。试试这个代码

    var hasAll = wList
     .Select(w => w.bearing)
     .Distinct()
     .Intersect(typeList)
     .Count() == typeList.Count;
    

    如果你需要 hasAll == true 只有当所有类型 wList 发生一次,删除对的调用 Distinct

    推荐文章