Home > Software engineering >  How to use LINQ to display variables in another class?
How to use LINQ to display variables in another class?

Time:12-02

I'm starting out with C# (two weeks in) and I'm trying to display a variable based on user input via Console.Readline(); method. This basic app is a calculator with two steps: Step 1 is a simple calculator; Step 2 should display material information (the name and price of the material).

The issue I'm having is in step 2 of my app, where I want to display the name and price of a material based on the user input. I'm not sure if LINQ is the best, easiest method for this. So open to how to solve this so it's readable by others and makes the program work.

Program CS. file is as follows:

using System;

namespace Gutterjob
{
    class Program
    {
        static void Main(String[] args)
        {
            Console.WriteLine("This is how much a gutter guard job will earn you!");
            Console.WriteLine("---------");
            Console.WriteLine("Step One:");
            var maths = new Calculator();
            maths.Main();
            
            Thread.Sleep(1500);

            Console.WriteLine("---------");
            Console.WriteLine("Step Two:");      
            Console.WriteLine("What type of gutter material are you using?");
            Console.WriteLine("Gold - [A]"); 
            Console.WriteLine("Silver - [B]");
            Console.WriteLine("Tin foil - [C]");
            
            //Have MaterialRepo data displayed here depending on user input.
            
        
            Console.ReadKey();
        }
    }
}

MaterialRepo.cs to house the material info

using System;
using System.Linq;
using System.Collections.Generic;

namespace Gutterjob
{
    public class MaterialRepo
    {
        public string Id;
        public string? Metal;
        public string Price;

        List<MaterialRepo> matList = new List<MaterialRepo>
        {
            new MaterialRepo {Id = "A", Metal = "Gold", Price = "$4"},
            new MaterialRepo {Id = "B", Metal = "Silver", Price = "$2"},
            new MaterialRepo {Id = "C", Metal = "Tin Foil", Price = "$1"},
        };
           
    }
}

I tried, but failed, to create a LINQ statement. Any help is greatly appreciated.

CodePudding user response:

What you're after is a Dictionary

Dictionary<string, MaterialRepo> matList = new Dictionary<string, MaterialRepo>(StringComparer.OrdinalIgnoreCase)
{
    {"A",  new MaterialRepo {Id = "A", Metal = "Gold", Price = "$4"} },
    {"B", new MaterialRepo {Id = "B", Metal = "Silver", Price = "$2" } },
    {"B", new MaterialRepo {Id = "C", Metal = "Tin Foil", Price = "$1"} },
};

Doing it this way lets you access items without linq

var itemA = matList["A"];

or even fancy stuff like

MaterialRepo myMaterial;
if (matList.TryGetValue("A", out myMaterial))
{
    //item exists
}
else
{ 
    //item doesn't
}

The stringcomparer argument in the constructor makes things case insensitive, up to you if you want that or not

CodePudding user response:

Program.cs

using System;

namespace Gutterjob
{
    class Program
    {
        static void Main(String[] args)
        {
            Console.WriteLine("This is how much a gutter guard job will earn you!");
            Console.WriteLine("---------");
            Console.WriteLine("Step One:");
            var maths = new Calculator();
            maths.Main();
        
            Thread.Sleep(1500);

            Console.WriteLine("---------");
            Console.WriteLine("Step Two:");      
            Console.WriteLine("What type of gutter material are you using?");
            Console.WriteLine("Gold - [A]"); 
            Console.WriteLine("Silver - [B]");
            Console.WriteLine("Tin foil - [C]");
        
            // this is my suggestion
            List<MaterialRepo> matList = new List<MaterialRepo>();
            matList.Add(new MaterialRepo { Id = "A", Metal = "Gold", Price = "$4" });
            matList.Add(new MaterialRepo { Id = "B", Metal = "Silver", Price = "$2" });
            matList.Add(new MaterialRepo { Id = "C", Metal = "Tin Foil", Price = "$1" });

            var input = Console.ReadLine();
            var selectedItem = matList.FirstOrDefault(x => x.Id.Equals(input));
            Console.WritLine($"{selectedItem.Metal}, {selectedItem.Price}");
        }
    }
}

And MaterialRepo.cs

public class MaterialRepo
{
    public string Id;
    public string Metal;
    public string Price;

    public MaterialRepo() { }
}

==========================================================

I recommend to you make 'matList' outside of MaterialRepo.cs. Because MaterialRepo.cs is single item class. So, if you try to make list of that class inside of same class, it is ridiculous.

If you want to make Main method clean, it is better to make other method only for initializing matList.

  • Related