CSharp · Console
using System;
using System.Collections.Generic;
using System.Linq;
class Program
{
public static List<Human> humans = new List<Human>()
{
new Human
{
FirstName="Karl",
LastName="Karlson",
clothings=new List<Clothing>()
{
new Clothing
{
Garment="T-Shirt",
Color="pink"
},
new Clothing
{
Garment="Pant",
Color="white"
},
new Clothing
{
Garment="Shoes",
Color="black"
}
}
},
new Human
{
FirstName="Paula",
LastName="Paulson",
clothings=new List<Clothing>()
{
new Clothing
{
Garment="T-Shirt",
Color="red"
},
new Clothing
{
Garment="Pant",
Color="blue"
},
new Clothing
{
Garment="Shoes",
Color="black"
},
new Clothing
{
Garment = "RandomGarment", //<- added a new clothing
Color = "RandomColor"
}
}
}
};
public static 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"
}
};
static void Main(string[] args)
{
var currentclothesAsHashSet = currentclothes.ToHashSet();
var human = humans.Where(x => !currentclothesAsHashSet.Except(x.clothings.OfType<CurrentClothes>().ToHashSet()).Any());
foreach (var human1 in human)
{
Console.WriteLine(human1.FirstName);
}
}
}
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);
}