Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to create a var with the use of anthoer variable name C#

    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;

}

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

>Solution :

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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading