Home > front end >  How to create a var with the use of anthoer variable name C#
How to create a var with the use of anthoer variable name C#

Time:11-11

    void Start()
        {
            var Match = from s in _supplier
                join b in _buyers on new { s.District } equals new { b.District }
                select new
                {
                    MatchSupplier = s,
                    MatchBuyer = b.Name
    
                };
            List<string> TempList = new List<string>();
            foreach (var VARIABLE in Match)
            {
                if (!TempList.Contains(VARIABLE.MatchSupplier.District))
                {
                    TempList.Add(VARIABLE.MatchSupplier.District);
                    
                }
                else
                {
                    Debug.Log(VARIABLE.MatchSupplier.District);
   // create a var with the name of the VARIABLE.MatchSupplier.District
   // create a var to record the number of each District repeating time
   
                }
                
            }
// Debug.log(${var(each District name)} {var(repeating time)})
    Debug.Log(TempList.Count);
           
        }

is it possible to do it all locally without creating a global variable? I want to record each district name and the number of each district name pop-up. Custom class

[Serializable] public class Supplier {

public string Name;
public string District;
public int Age;

}

[Serializable]
public class Buyer
{
    public string Name;
    public string District;
    public int Age;
}

CodePudding user response:

I think you want to count certain items so you would probbaly rather use a Dictionary<string, int> and do something like e.g.

Dictionary<string, int> TempList = new();

foreach (var VARIABLE in Match)
{
    var district = VARIABLE.MatchSupplier.District;
    TempList.TryGetValue(district, out var count);
    TempList[district] = count   1;
}  

foreach(var kvp in TempList)
{
    Debug.Log($"{kvp.Key} - {kvp.Value}");
}

Slightly simplified Fiddle

  • Related