简体   繁体   中英

C# Compare two list and return value

I have a problem. I try compare two list currentItemsInColl and bPList . Inside bPList i have other list RequiredItems and now is what I need. I want compare currentItemsInColl and RequiredItems and return bPList.craftingBlueprint. I try Compare but I dont know how use it :/

using Devdog.InventoryPro;
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;


public class CraftingAutoUpdate : MonoBehaviour {

    public ItemCollectionBase itemCollection;
    public ItemCollectionBase rewardCollection;
    public CraftingCategory craftingCategory;

    [Header("Blue Print List")]

    public List<BlueprintList> bPList = new List<BlueprintList>();
    public List<CurrentItemInCollList> currentItemsInColl = new List<CurrentItemInCollList>();

    private CraftingBlueprint readyBlueprint;

    public void OnShow()
    {
        GetBluePrint();
        InvokeRepeating("StartUpdate",0f,0.05f);
    }

    public void OnHide()
    {
        CancelInvoke("StartUpdate");
    }

    private void StartUpdate()
    {

        UpdateDirectory();
        UpdateFindMatchItems();
        UpdateCraftResults();
    }


    private void GetBluePrint()
    {
        bPList.Clear();
        foreach (var b in craftingCategory.blueprints)
        {
            if (b != null)
            {
                var rI = b.requiredItems;

                var listReqItems = new List<RequiredItem>();
                foreach (var e in rI)
                {
                    listReqItems.Add(new RequiredItem(e.item.ID, e.amount));
                }

                bPList.Add(new BlueprintList(b.name, b, listReqItems));
            }
        }
    }

    private void UpdateDirectory()
    {
        currentItemsInColl.Clear();

        foreach(var item in itemCollection)
        {
            if (item.item != null)
            {
                var cT = item.item.ID;
                if (currentItemsInColl.Find(u =>u.itemID == cT) == null)
                {
                    var itemCount = itemCollection.GetItemCount(item.item.ID);
                    currentItemsInColl.Add(new CurrentItemInCollList(item.item.ID, itemCount));
                }
            }
        }
    }

In this methode I try find same items in collections:

    private void UpdateFindMatchItems()
    {
        readyBlueprint = null;
        bool matchFailed = false;
        int requiredItemCount = 0;
        int currentItemsInCollCount = currentItemsInColl.Count;

        foreach(var bp in bPList)
        {

            requiredItemCount = bp.RequiredItems.Count;
            foreach(var rI in bp.RequiredItems)
            {

               if(CompareLists(currentItemsInColl, bp.RequiredItems))
                {
                    print("aa");
                }

            print(currentItemsInCollCount);
        }
    }

    private void UpdateCraftResults()
    {
        rewardCollection.Clear();
        if (readyBlueprint != null)
        {
            foreach (var items in readyBlueprint.resultItems)
            {
                rewardCollection.AddItem(items.item,null,true,false);
            }
        }
    }

I try somthing like this but is wont work with this lists:

    public static bool CompareLists<T>(List<T> aListA, List<T> aListB)
    {
        if (aListA == null || aListB == null || aListA.Count != aListB.Count)
            return false;
        if (aListA.Count == 0)
            return true;
        Dictionary<T,T> lookUp = new Dictionary<T,T>();
        // create index for the first list
        for (int i = 0; i < aListA.Count; i++)
        {
            uint count = 0;
            if (!lookUp.TryGetValue(aListA[i], out count))
            {
                lookUp.Add(aListA[i], 1);
                continue;
            }
            lookUp[aListA[i]] = count + 1;
        }
        for (int i = 0; i < aListB.Count; i++)
        {
            uint count = 0;
            if (!lookUp.TryGetValue(aListB[i], out count))
            {
                // early exit as the current value in B doesn't exist in the lookUp (and not in ListA)
                return false;
            }
            count--;
            if (count <= 0)
                lookUp.Remove(aListB[i]);
            else
                lookUp[aListB[i]] = count;
        }
        // if there are remaining elements in the lookUp, that means ListA contains elements that do not exist in ListB
        return lookUp.Count == 0;
    }
}

And this is my lists:

/*     LISTS     */


[Serializable]
public class CurrentItemInCollList
{
    public uint itemID;
    public uint itemAmount;

    public CurrentItemInCollList(uint newitemID, uint newItemAmount)
    {
        itemID = newitemID;
        itemAmount = newItemAmount;
    }
}

[Serializable]
public class BlueprintList
{
    public string bluePrintName;
    public CraftingBlueprint craftingBlueprint;
    public List<RequiredItem> RequiredItems = new List<RequiredItem>();

    public BlueprintList(string newBluePrintName, CraftingBlueprint newcraftingBlueprint, List<RequiredItem> list)
    {
        bluePrintName = newBluePrintName;
        craftingBlueprint = newcraftingBlueprint;
        RequiredItems = list;

    }
}
[Serializable]
public class RequiredItem
{
    public uint itemID;
    public uint itemAmount;

    public RequiredItem( uint newitemID, uint newItemAmount)
    {
        itemID = newitemID;
        itemAmount = newItemAmount;
    }
}

I forgot.. CurrentItemInCollList.itemAmount can be >= RequiredItems.itemAmount

Dictionary use hash values to compare objects. The stored classes must implement public override int GetHashCode(){}

Use Linq - here is a small console example:

class Program
{
    static void Main(string[] args)
    {
        //Required list
        List<Order> currentItemsInColl = new List<Order>();
        currentItemsInColl.Add(new Order() { Name = "bike1", Id = "01" });
        currentItemsInColl.Add(new Order() { Name = "bike4", Id = "04" });

        //List of all items
        List<BPP> bPList = new List<BPP>();
        bPList.Add(new BPP() { BikeName = "bike1", Idzzz = "01" });
        bPList.Add(new BPP() { BikeName = "bike2", Idzzz = "02" });
        bPList.Add(new BPP() { BikeName = "bike3", Idzzz = "03" });
        bPList.Add(new BPP() { BikeName = "bike4", Idzzz = "04" });
        bPList.Add(new BPP() { BikeName = "bike5", Idzzz = "05" });

        //Blueprint List
        List<BPP> Blueprint = new List<BPP>();

        //get all items into the Blueprint list
        foreach (Order i in currentItemsInColl)
        {
            List<BPP> tmp = bPList.FindAll(x => x.Idzzz.Contains(i.Id));
            //here you add them all to a list
            foreach (BPP item in tmp)
            {
                Blueprint.Add(item);
            }
        }

        Console.ReadLine();
    }

}
public class Order
{
    public string Id { get; set; }
    public string Name { get; set; }
}

public class BPP
{
    public string Idzzz { get; set; }
    public string BikeName { get; set; }
}

Sidenote: i am comparing the ID's in each of the lists! Hope it helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM