代码之家  ›  专栏  ›  技术社区  ›  geoff swartz

如何创建此Linq查询

  •  1
  • geoff swartz  · 技术社区  · 14 年前

    我有一种情况,我有一个ID列表,如1、2、3、4。我需要将另一个ID列表传递给一个方法,如果列表中的数字相同,则返回true;否则,如果两个列表不相同(忽略排序),则返回false。因此,使用1、2、3、4、5的方法调用应返回false,而使用2、4、1、3_的调用返回true。这听起来很简单,但我不知道怎么做。

    2 回复  |  直到 14 年前
        1
  •  0
  •   jdmichal    14 年前

    idList

    if (idList.Count == otherIDList.Count &&
        idList.Intersect(otherIDList).Count() == idList.Count)
    {
        // Contain same things.
    }
    else
    {
        // Do not contain same things.
    }
    

    otherIDList

        2
  •  2
  •   Jon Skeet    14 年前

    var idSet = new HashSet<int>(idList1);
    if (idSet.SetEquals(idList2))
    {
        ...
    }