按特定属性比较两个列表

Compare two Lists by specific properties

我有一个列表中的列表。我需要两个将 sub_List 的两个属性与第三个列表进行比较。

类:

public class Human
{
    public string FirstName{ get; set; }
    public string LastName{ get; set; }
    public List<Clothing> clothings { get; set; }
}

public class Clothing
{
    public string ClothingID{ get; set; }
    public string Garment{ get; set; }
    public string Color{ get; set; }
}

public class CurrentClothes
{
    public string Garment{ get; set; }
    public string Color{ get; set; }
}

伪代码:

public List<Human> humans = new List<Human>()
{
    new Human
    {
        FirstName="Karl",
        LastName="Karlson"
        clothings=new List<Clothing>()
        {
            new Clothing
            {
                ClothingID="1",
                Garment="T-Shirt",
                Color="pink"
            },
            new Clothing
            {
                ClothingID="11",
                Garment="Pant",
                Color="white"
            },
            new Clothing
            {
                ClothingID="111",
                Garment="Shoes",
                Color="black"
            }
        }
    },
    new Human
    {
        FirstName="Paula",
        LastName="Paulson"
        clothings=new List<Clothing>()
        {
            new Clothing
            {
                ClothingID="2",
                Garment="T-Shirt",
                Color="red"
            },
            new Clothing
            {
                ClothingID="22",
                Garment="Pant",
                Color="blue"
            },
            new Clothing
            {
                ClothingID="222"
                Garment="Shoes",
                Color="black"
            }
        }
    }
};
    
public List<CurrentClothes> currentclothes = new List<CurrentClothes>()
{
    new CurrentClothes
    {
        Garment="Pant",
        Color="blue"
    },
    new CurrentClothes
    {
        Garment="T-Shirt",
        Color="red"
    },
    new CurrentClothes
    {
        Garment="Shoes",
        Color="black"
    }
}

var human = humans.Where(x=>x.clothings.Equals(currentClothes));

问题是,如何比较当前的衣服是否与人类的衣服相匹配。是否有任何 Linq 选项?

我添加了一个示例。在这个例子中有两个人。卡尔和宝拉。当前的衣服是确定的。现在我想要人类如何匹配当前的衣服。在这种情况下,Paula.

您可以这样使用 Any

    humans.clothings.Where(x => 
       currentClothes.Any(s => 
        s.Color.Equals(x.Color) && s.Garment.Equals(x.Garment)
       )
    ).ToList();

检查 this 以获得 .netFiddle 演示

我会做以下事情

public class Human
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public List<Clothing> clothings { get; set; }
}

public class Clothing : CurrentClothes
{
    public string ClothingID { get; set; }
}

public class CurrentClothes : IEquatable<CurrentClothes>
{
    public string Garment { get; set; }
    public string Color { get; set; }

    public override bool Equals(object obj) => ReferenceEquals(this, obj) || obj is CurrentClothes other && Equals(other);
    public bool Equals(CurrentClothes other)
    {
        if (ReferenceEquals(null, other)) return false;
        if (ReferenceEquals(this, other)) return true;
        return Garment == other.Garment && Color == other.Color;
    }
    public override int GetHashCode() => HashCode.Combine(Garment, Color);
}

然后像这样使用它:

var currentclothesAsHashSet = currentclothes.ToHashSet();
var human = humans.Where(x => x.clothings.OfType<CurrentClothes>().ToHashSet().SetEquals(currentclothesAsHashSet));
foreach (var human1 in human)
{
    Console.WriteLine(human1.FirstName);
}

这里使用了

的方法

https://docs.microsoft.com/en-us/dotnet/api/system.collections.generic.iset-1.setequals

查看 Human.clothings 是否与 currentclothes 相同。

这是一个演示: https://dotnetfiddle.net/tS72Wt

编辑:

正如评论中指出的那样,如果 currentClothes 只能是数据的一个子集,您可能需要将 SetEquals 更改为 Except

就像这个演示演示的那样:

https://dotnetfiddle.net/FZg85Z