I started comparing lists for my little project. So far I can compare if the same items exists, or one value of this item fits the same value of another lists item. Atm Im comparing the names from two lists. I changed some code I found for my purpose.
public class TestResources
{
public string Name;
public int Amount;
public TestResources(string name, int amount)
{
Name = name;
Amount = amount;
}
}
TestResources[] ListResInStock = { new TestResources("wood", 1000), new TestResources("stone", 1000), new TestResources("sand", 1000), new TestResources("water", 1000) };
TestResources[] ListResNeeded = { new TestResources("wood", 800), new TestResources("stone", 800), new TestResources("sand", 375) };
private bool ContainsResourceName(IEnumerable<TestResources> ListResNeeded, IEnumerable<TestResources> ListResInStock)
{
bool result;
var list1WithName = ListResNeeded.Select(s => s.Name).ToList();
var list2WithName = ListResInStock.Select(s => s.Name).ToList();
result = !list1WithName.Except(list2WithName).Any();
return result;
}
the function is called with:
Console.WriteLine("ListResInStock contains ListResNeeded: ===> " + ContainsResourceName(ListResNeeded, ListResInStock) + " <==="); // True
Console.WriteLine("ListResNeeded contains ListResInStock: ===> " + ContainsResourceName(ListResInStock, ListResNeeded) + " <==="); // False
Now I want to change it, so that it only returns true, if the name of the ressNeeded is there and the amount of ressInStock is >= the Amount of RessNeeded (for all resources of course).
Also If you could explain this part var list1WithName = ListResNeeded.Select(s => s.Name).ToList(); In detail to me would be nice because im not 100% sure what it does.
I really appreciate your help :-)