LINQ查询以查找列表1中是否包含列表1中的所有项目

时间:2015-08-25 20:44:43

标签: c# linq

我试图关注然后扩展一个旧示例Linq query list contains a list,但它对我没用。

class Part
{
    public int id { get; set; }
    public string name { get; set; }
}

class Program
{
    static void Main(string[] args)
    {
        {
            List<int> L1 = new List<int> { 1, 2, 3, 4, 5, 6 };
            List<int> L2 = new List<int> { 4, 3 };

            bool t = L2.Where(p => L2.All(q => L1.Contains(q))).Any();
        }



        {
            List<Part> L1 = new List<Part> { new Part { id = 1 }, new Part { id = 2 }, new Part { id = 3 }, new Part { id = 4 } };
            List<Part> L2 = new List<Part> { new Part { id = 3 }, new Part { id = 4 } };

            bool u = L2.Where(p => L2.All(q => L1.Contains(q.id))).Any();
        }
    }
}

第一个测试对我有用,但与我找到的早期代码完全匹配。我的第二个测试在&#34; L1.Contains(q.id)&#34;处有语法错误。我很难过。

4 个答案:

答案 0 :(得分:0)

L1List<Part>qPartq.id是int。

L1无法Contain int

类型的项目

要检查L1是否包含具有该ID的项目,请使用Any

L2.All(q => L1.Any(e => e.id == q.id))

答案 1 :(得分:0)

使用等于运算符时,您要检查列表是否包含对相同对象的引用。如果你想匹配那么你必须通过选择要比较的值来做你喜欢的事情。您的语法错误来自于您将ID与零件进行比较。如果你将L1投射到ids(L1.Select(p => p.Id)),你应该是好的。

答案 2 :(得分:0)

For&#34; IsContained&#34;问题:

using System;
using System.Collections.Generic;
using System.Linq;

public class Program
{
    class Part
    {
        public int id { get; set; }
        public string name { get; set; }
    }

    public static void Main()
    {
        {
            var L1 = new List<int> { 1, 2, 3, 4, 5, 6 };
            var L2 = new List<int> { 4, 3 };

            bool t = L2.All(l2 => L1.Contains(l2));

            Console.WriteLine("L1 contains L2: {0}", t);
        }

        {
            var L1 = new List<Part> { new Part { id = 1 }, new Part { id = 2 }, new Part { id = 3 }, new Part { id = 4 } };
            var L2 = new List<Part> { new Part { id = 3 }, new Part { id = 4 } };

            bool u = L2.All(l2 => L1.Any(l1 => l1.id == l2.id));

            Console.WriteLine("L1 contains L2: {0}", u);
        }
    }
}

答案 3 :(得分:0)

你不能在那里使用Contains,因为类型是List<Part>,至少没有某种投射或不同的参数。如果您通常会将这些类型与列表相交,我建议您为此类部分实施IEquatable<T>;

class Part : IEquatable<Part>
{
    public int id { get; set; }
    public string name { get; set; }

    public bool Equals(Part other)
    {

        //Check whether the compared object is null.
        if (Object.ReferenceEquals(other, null)) return false;

        //Check whether the compared object references the same data.
        if (Object.ReferenceEquals(this, other)) return true;

        //Check whether the products' properties are equal.
        return id.Equals(other.id) && name.Equals(other.name);
    }

    // If Equals() returns true for a pair of objects 
    // then GetHashCode() must return the same value for these objects.

    public override int GetHashCode()
    {

        //Get hash code for the Name field if it is not null.
        int nameHash = name == null ? 0 : name.GetHashCode();

        int idHash = id.GetHashCode();

        //Calculate the hash code for the part.
        return nameHash ^ idHash;
    }
}

这样,您只需执行L1.Intersect(L2)即可获得预期结果。如果你打算这样做一次可能更容易写出类似的东西; L2.All(q => L1.Any(e => e.id == q.id))虽然我不建议重复使用它代替Intersect

相关问题