Home > Back-end >  See if a single string is containing something similar in the List with custom Class
See if a single string is containing something similar in the List with custom Class

Time:07-30

My question is, I'm trying to compare if the anchor.name are containing anything in the List. The problem is the list is with a custom class, and I only want to see if the name have contain anything in the GUID that is in the List.

         anchor.name = $"Anchor{id}{anchor.name}";
            var tem = gameData_List.showing_my_loading_list.Select(x => x.guid);
            
            if (anchor.name.Contains(tem.ToString()))
            {
                Debug.Log("Spotted Saved GameOBJ");
            }

The Class variables

[Serializable]
public class MySecondGameList
{
    public Guid guid;
    public string readable_guid;
    //public GameObject anchor_gameobject;
}

Right now when I Debug, nothing shows up so I guess I messed something up here. Because I know that it is a match since I can see the GUID in the inspector knowing that anchor.name is contained.

CodePudding user response:

I would modify your code like that:

anchor.name = $"Anchor{id}{anchor.name}";
var exists = gameData_List.showing_my_loading_list
    .Any(x => anchor.name.Contains(x.guid.ToString());

if (exists)
{
    Debug.Log("Spotted Saved GameOBJ");
}

CodePudding user response:

This code returns a list var tem = gameData_List.showing_my_loading_list.Select(x => x.guid);

You're trying to make the list guid here if (anchor.name.Contains(tem.ToString())), but it won't return a value. You get type info, for example System.Collections.Generic.List'1[System.Guid]

If you need one you can use eg: var tem = gameData_List.showing_my_loading_list.Select(x => x.guid).FirstOrDefault();

  • Related